Java實現簡單郵件發送功能
本文實例為大傢分享瞭Java實現簡單郵件發送的具體代碼,供大傢參考,具體內容如下
需要的jar包:
- activation-1.1.1.jar
- mail-1.4.7.jar
QQ郵箱設置開啟POP3/SMTP服務,並獲得授權碼
java實現簡單郵件發送
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 { //要發送郵件,需要獲得協議和支持!開啟服務POP3/SMTP服務 授權碼: fsxqgovorymigfeb Properties prop=new Properties(); prop.setProperty("mail.host","smtp.qq.com");//設置QQ郵件服務器 prop.setProperty("mail.transport.protocol","smtp");//設置郵箱發送協議 prop.setProperty("mail.smtp.auth","true");//需要驗證用戶名密碼 //QQ郵箱還有設置SSL加密 MailSSLSocketFactory sf=new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //1.創建定義整個應用程序所需要的環境信息的Session對象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]","fsxqgovorymigfeb"); } }); //開啟session的debug模式,這樣就可以查看運行狀態瞭 session.setDebug(true); //2.通過session對象獲得transport對象 Transport transport = session.getTransport(); //3.使用郵箱的用戶名和授權碼連上郵件服務器 transport.connect("smtp.qq.com","[email protected]","fsxqgovorymigfeb"); //4.創建郵件:寫郵件 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("[email protected]"));//發件人 message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));//收件人 message.setSubject("你好");//郵件主題 message.setContent("<h1 style='color: red'>你好</h1>","text/html;charset=utf-8");//郵件內容 //5.發送郵件 transport.sendMessage(message,message.getAllRecipients()); //6.關閉連接 transport.close(); } }
java實現復雜郵件發送( 帶文件 )
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 { //要發送郵件,需要獲得協議和支持!開啟服務POP3/SMTP服務 授權碼: fsxqgovorymigfeb Properties prop=new Properties(); prop.setProperty("mail.host","smtp.qq.com");//設置QQ郵件服務器 prop.setProperty("mail.transport.protocol","smtp");//設置郵箱發送協議 prop.setProperty("mail.smtp.auth","true");//需要驗證用戶名密碼 //QQ郵箱還有設置SSL加密 MailSSLSocketFactory sf=new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); //1.創建定義整個應用程序所需要的環境信息的Session對象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]","fsxqgovorymigfeb"); } }); //開啟session的debug模式,這樣就可以查看運行狀態瞭 session.setDebug(true); //2.通過session對象獲得transport對象 Transport transport = session.getTransport(); //3.使用郵箱的用戶名和授權碼連上郵件服務器 transport.connect("smtp.qq.com","[email protected]","fsxqgovorymigfeb"); //4.創建郵件:寫郵件 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("[email protected]"));//發件人 message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));//收件人 message.setSubject("你好");//郵件主題 //message.setContent("<h1 style='color: red'>你好</h1>","text/html;charset=utf-8");//郵件內容 //============================================================================= //帶圖片的內容 MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("E:\\IDEA\\JavaWeb\\mail-java\\src\\tx.png"));//圖片需要經過數據處理... DataHandler:數據處理 image.setDataHandler(dh);//在Body中放入處理的圖片數據 image.setContentID("tx.png");//給圖片設置ID //準備正文數據 MimeBodyPart text = new MimeBodyPart(); text.setContent("這是一封郵件正文帶圖片<img src='cid:tx.png'>的郵件","text/html;charset=utf-8"); //描述數據關系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("mixed"); //設置到消息中,保存修改 message.setContent(mm); message.saveChanges(); //========================================================================= //5.發送郵件 transport.sendMessage(message,message.getAllRecipients()); //6.關閉連接 transport.close(); } }
Spring實現
1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、編寫配置文件
[email protected] 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() { //發送郵件 //收件人 //內容 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("測試"); message.setText("Hello"); message.setFrom("[email protected]"); message.setTo("[email protected]"); mailSender.send(message); } @Test public void test2() throws Exception {//復雜郵件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("測試"); helper.setText("Hello",true); //附件 helper.addAttachment("1.jpg",new File("")); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); mailSender.send(mimeMessage); } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。