您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java如何實現(xiàn)簡單郵件發(fā)送功能”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
需要的jar包:
activation-1.1.1.jar
mail-1.4.7.jar
QQ郵箱設(shè)置開啟POP3/SMTP服務(wù),并獲得授權(quán)碼
java實現(xiàn)簡單郵件發(fā)送
import com.sun.mail.util.MailSSLSocketFactory; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class Mail1 { public static void main(String[] args) throws Exception { //要發(fā)送郵件,需要獲得協(xié)議和支持!開啟服務(wù)POP3/SMTP服務(wù) 授權(quán)碼: fsxqgovorymigfeb Properties prop=new Properties(); prop.setProperty("mail.host","smtp.qq.com");//設(shè)置QQ郵件服務(wù)器 prop.setProperty("mail.transport.protocol","smtp");//設(shè)置郵箱發(fā)送協(xié)議 prop.setProperty("mail.smtp.auth","true");//需要驗證用戶名密碼 //QQ郵箱還有設(shè)置SSL加密 MailSSLSocketFactory sf=new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //1.創(chuàng)建定義整個應(yīng)用程序所需要的環(huán)境信息的Session對象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("1369410772@qq.com","fsxqgovorymigfeb"); } }); //開啟session的debug模式,這樣就可以查看運行狀態(tài)了 session.setDebug(true); //2.通過session對象獲得transport對象 Transport transport = session.getTransport(); //3.使用郵箱的用戶名和授權(quán)碼連上郵件服務(wù)器 transport.connect("smtp.qq.com","1369410772@qq.com","fsxqgovorymigfeb"); //4.創(chuàng)建郵件:寫郵件 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("1369410772@qq.com"));//發(fā)件人 message.setRecipient(Message.RecipientType.TO,new InternetAddress("1369410772@qq.com"));//收件人 message.setSubject("你好");//郵件主題 message.setContent("<h2 style='color: red'>你好</h2>","text/html;charset=utf-8");//郵件內(nèi)容 //5.發(fā)送郵件 transport.sendMessage(message,message.getAllRecipients()); //6.關(guān)閉連接 transport.close(); } }
java實現(xiàn)復(fù)雜郵件發(fā)送( 帶文件 )
import com.sun.mail.util.MailSSLSocketFactory; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Properties; public class Mail1 { public static void main(String[] args) throws Exception { //要發(fā)送郵件,需要獲得協(xié)議和支持!開啟服務(wù)POP3/SMTP服務(wù) 授權(quán)碼: fsxqgovorymigfeb Properties prop=new Properties(); prop.setProperty("mail.host","smtp.qq.com");//設(shè)置QQ郵件服務(wù)器 prop.setProperty("mail.transport.protocol","smtp");//設(shè)置郵箱發(fā)送協(xié)議 prop.setProperty("mail.smtp.auth","true");//需要驗證用戶名密碼 //QQ郵箱還有設(shè)置SSL加密 MailSSLSocketFactory sf=new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //1.創(chuàng)建定義整個應(yīng)用程序所需要的環(huán)境信息的Session對象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("1369410772@qq.com","fsxqgovorymigfeb"); } }); //開啟session的debug模式,這樣就可以查看運行狀態(tài)了 session.setDebug(true); //2.通過session對象獲得transport對象 Transport transport = session.getTransport(); //3.使用郵箱的用戶名和授權(quán)碼連上郵件服務(wù)器 transport.connect("smtp.qq.com","1369410772@qq.com","fsxqgovorymigfeb"); //4.創(chuàng)建郵件:寫郵件 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("1369410772@qq.com"));//發(fā)件人 message.setRecipient(Message.RecipientType.TO,new InternetAddress("1369410772@qq.com"));//收件人 message.setSubject("你好");//郵件主題 //message.setContent("<h2 style='color: red'>你好</h2>","text/html;charset=utf-8");//郵件內(nèi)容 //============================================================================= //帶圖片的內(nèi)容 MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("E:\\IDEA\\JavaWeb\\mail-java\\src\\tx.png"));//圖片需要經(jīng)過數(shù)據(jù)處理... DataHandler:數(shù)據(jù)處理 image.setDataHandler(dh);//在Body中放入處理的圖片數(shù)據(jù) image.setContentID("tx.png");//給圖片設(shè)置ID //準(zhǔn)備正文數(shù)據(jù) MimeBodyPart text = new MimeBodyPart(); text.setContent("這是一封郵件正文帶圖片<img src='cid:tx.png'>的郵件","text/html;charset=utf-8"); //描述數(shù)據(jù)關(guān)系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("mixed"); //設(shè)置到消息中,保存修改 message.setContent(mm); message.saveChanges(); //========================================================================= //5.發(fā)送郵件 transport.sendMessage(message,message.getAllRecipients()); //6.關(guān)閉連接 transport.close(); } }
Spring實現(xiàn)
1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、編寫配置文件
spring.mail.username=1369410772@qq.com spring.mail.password=fsxqgovorymigfeb spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true
3、編寫測試類
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class DemoApplicationTests {//簡單郵件 @Autowired JavaMailSenderImpl mailSender; @Test void contextLoads() { //發(fā)送郵件 //收件人 //內(nèi)容 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("測試"); message.setText("Hello"); message.setFrom("1369410772@qq.com"); message.setTo("1369410772@qq.com"); mailSender.send(message); } @Test public void test2() throws Exception {//復(fù)雜郵件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("測試"); helper.setText("Hello",true); //附件 helper.addAttachment("1.jpg",new File("")); helper.setFrom("1369410772@qq.com"); helper.setTo("1369410772@qq.com"); mailSender.send(mimeMessage); } }
“Java如何實現(xiàn)簡單郵件發(fā)送功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。