java發送郵件的代碼

來源:互聯網
上載者:User

標籤:郵件   throws   span   java   logs   建立   internet   tor   sendmail   

package com.dong.mail;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;public class MailUtils {    //email:郵件發給誰  subject:主題  emailMsg:郵件的內容    public static void sendMail(String email, String subject, String emailMsg)            throws AddressException, MessagingException {                // 1.建立一個程式與郵件伺服器會話對象 Session        Properties props = new Properties();        props.setProperty("mail.transport.protocol", "SMTP");//發郵件的協議        props.setProperty("mail.host", "smtp.163.com");//發送郵件的163伺服器位址        props.setProperty("mail.smtp.auth", "true");// 指定驗證為true        // 建立驗證器        Authenticator auth = new Authenticator() {            public PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("xxx", "mima");//發郵件的帳號的驗證            }        };        Session session = Session.getInstance(props, auth);        // 2.建立一個Message,它相當於是郵件內容        Message message = new MimeMessage(session);        message.setFrom(new InternetAddress("[email protected]")); // 設定寄件者        message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設定發送方式與接收者        message.setSubject(subject);//郵件的主題        message.setContent(emailMsg, "text/html;charset=utf-8");        // 3.建立 Transport用於將郵件發送        Transport.send(message);    }}

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.