java使用spring實現發送mail的方法_java

來源:互聯網
上載者:User

本文執行個體講述了java使用spring實現發送mail的方法。分享給大家供大家參考。具體如下:

這裡借鑒別人的優點以及自己的一些加工,寫出如下代碼:

package test;import java.util.Properties;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;/** * 本類測試簡單郵件 直接用郵件發送 *  * @author Administrator *  */public class SingleMailSend { public static void main(String args[]) throws MessagingException { JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); // 設定郵件伺服器 //senderImpl.setHost("smtp.163.com"); senderImpl.setHost("smtp.qq.com"); // 建立郵件訊息 // SimpleMailMessage mailMessage = new SimpleMailMessage(); MimeMessage mailMessage = senderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mailMessage,true,"utf-8"); // 設定收件者,群發郵件 String[] array = new String[] {"18601463269@126.com","1965571954@qq.com"}; helper.setTo(array); //mailMessage.setTo("iamzken@163.com"); helper.setFrom("1965571954@qq.com"); helper.setSubject("這是我的主題!"); helper.setText("<p style='color:red;'>這是我的內容!</p>",true); //添加附件 ClassPathResource resource = new ClassPathResource("test.jpg");  helper.addAttachment("hello.jpg", resource); // 根據自己的情況,設定username //senderImpl.setUsername("iamzken@163.com");  senderImpl.setUsername("1965571954@qq.com");  senderImpl.setPassword("你的密碼"); // 根據自己的情況, 設定password /* Properties prop = new Properties(); prop.put(" mail.smtp.auth ", " true "); // 將這個參數設為true,讓伺服器進行認證,認證使用者名稱和密碼是否正確 prop.put(" mail.smtp.timeout ", " 25000 "); senderImpl.setJavaMailProperties(prop);*/ // 發送郵件 senderImpl.send(mailMessage); System.out.println(" 郵件發送成功.. "); }}

希望本文所述對大家的java程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.