運用Java Mail向notes郵箱發郵件!

來源:互聯網
上載者:User

/*
 * Created on 2009-1-8
 */
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author gjg
 */
public class SenderWithSMTPVer {
 String host = "82.0.191.1";

 /**
  *
  * @param from
  *            寄件者的地址
  * @param to
  *            收件者地址
  * @param titleMsg
  *            郵件標題
  */
 public void sendMail(String from, String to, String titleMsg) {

  Properties props = new Properties();
  props.put("mail.smtp.host", host);// 指定SMTP伺服器
  props.put("mail.smtp.auth", "false");// 指定是否需要SMTP驗證,為false時,不用指定使用者名稱、密碼
  try {
   Session mailSession = Session.getDefaultInstance(props);

   mailSession.setDebug(true);// 是否在控制台顯示debug資訊

   MimeMessage message = new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from));// 寄件者
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     to));// 收件者
   // 將中文轉化為GB2312編碼
   message.setSubject(titleMsg, "GB2312"); // 郵件主題
   message.setContent("<a href='http://www.qq.com'>請點擊此連結!</a>",
     "text/html;charset=gb2312");// 郵件內容
   message.saveChanges();

   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, null, null);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static void main(String args[]) {
  SenderWithSMTPVer sm = new SenderWithSMTPVer();
  sm.sendMail("abc@sohu.com", "kjc_zh/xj/ccb@ccb", "Test Mail!");
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.