Java发邮件

就算是一个非常简陋的客户端吧。用163.com发送邮件

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      //String to = "liulixin.org@gmail.com";
	   String to = "18802930979@qq.com";
	  //往谷歌邮箱发可以,往qq邮箱被退回
	  //第二次往qq发可以了

      // Sender's email ID needs to be mentioned
      String from = "liulixin63@163.com";

      // Assuming you are sending email from localhost
      String host = "smtp.163.com";

      // Get system properties
      Properties properties = new Properties();

      // Setup mail server
      properties.put("mail.transpost.protocol","smtp");
      properties.put("mail.smtp.host", "smtp.163.com");
      properties.put("mail.smtp.auth", "true");
      properties.put("mail.smtp.port","25");
      //properties.put("mail.user", "bGl1bGl4aW42Mw==");
      //properties.put("mail.password", "*********");
      
      MailAuthenticator authenticator = new MailAuthenticator("liulixin63", "******");
      

      // Get the default Session object.
      Session session = Session.getInstance(properties, authenticator);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class MailAuthenticator extends Authenticator {
 
    
    private String username;
   
    private String password;
 
    public MailAuthenticator(String username, String password) {
    this.username = username;
    this.password = password;
    }
 
    String getPassword() {
    return password;
    }
 
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password);
    }
 
    String getUsername() {
    return username;
    }
 
    public void setPassword(String password) {
    this.password = password;
    }
 
    public void setUsername(String username) {
    this.username = username;
    }
 
}

函库源代码:http://pan.baidu.com/share/link?shareid=431484&uk=3878681452

参考:http://188029.net/java/j12-04-01u.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值