`
angkorpeach
  • 浏览: 119878 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java发送邮件

阅读更多

此段代码是我的同事胡成的作品。

 

package com.zrar.util;
/**邮件发送
 * 胡成
 * 2008-07-11
 */
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 


public class SendingEmail 
{ 
    public static void main(String args[]) throws Exception 
    { 
        String host = "ahldt.gov.cn"; //smtp.mail.yahoo.com  smtp.sina.com.cn  smtp.sohu.com
        String from =  "12333test@ahldt.gov.cn"; 
        String to = "hczjcl@qq.com"; 
        String username = "12333test@ahldt.gov.cn"; 
        String password = "123333"; 

        // Get system properties 
        // Properties props = System.getProperties();
        Properties props = new Properties(); 

        // Setup mail server 
        props.put("mail.smtp.host", host); 
        props.put("mail.smtp.auth", "true"); //这样才能通过验证 
        // Get session 
        Session session = Session.getDefaultInstance(props); 
        // watch the mail commands go by to the mail server 
        session.setDebug(true); 
        // Define message 
        MimeMessage message = new MimeMessage(session); 
        message.setFrom(new InternetAddress(from)); 
        message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to)); 
        message.setSubject("安徽12333给你的回复"); 
        message.setText("Welcome to JavaMail   \r\n 换行没"); 
        // Send message 
        message.saveChanges(); 
        Transport transport = session.getTransport("smtp"); 
        transport.connect(host, username, password); 
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close(); 
    } 
}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics