首页 > 代码库 > 实现发送电子邮件

实现发送电子邮件

原文出处:http://www.cnblogs.com/AndroidJotting/p/5024124.html

1、   jar:技术分享

链接:http://pan.baidu.com/s/1mia8IVm 密码:bypd

2、  开启第三方登录;

技术分享

技术分享

3、可以写代码了;

public class PopupAuthenticator extends Authenticator {
    public static final String mailuser = "11111111";//发送方邮箱‘@‘符号前的内容:1111@qq.com
    //	   public static final String mailuser = "mailteam";//发送方邮箱‘@‘符号前的内容:1111@qq.com
    public static final String password = "uadm********bhd";//成功开启IMAP/SMTP服务,在第三方客户端登录时,腾讯提供的密码。注意不是邮箱密码

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(mailuser, password);
    }

}

public class Mail {
@Test
public void testMail() {
String acceptAddressMM = "982977248@qq.com";//接收方邮箱地址的标签
String labelMM = "我是发送邮件方的标签";
String acceptlabelMM = "我是接受邮件方的标签";
String subjectMM = "我是邮件主题";
String textMM = "我是邮件内容";


String stateMM = this.sendMail(labelMM, acceptlabelMM, acceptAddressMM, subjectMM, textMM);
//将发送的信息保存到数据库
this.saveMailMessage(labelMM, acceptlabelMM, acceptAddressMM, subjectMM, textMM, stateMM);
}

/**
* @param labelMM 发送方邮箱地址的标签
* @param acceptlabelMM 接收方邮箱地址的标签
* @param acceptAddressMM 接收方电子邮箱地址
* @param subjectMM 发送电子邮件的主题
* @param textMM 发送电子邮件内容
* @return
*/
public String sendMail(String labelMM, String acceptlabelMM, String acceptAddressMM, String subjectMM, String textMM) {

String stateMM = null;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
// props.put("mail.smtp.port", "465");//使用465或587端口
props.put("mail.smtp.port", "587");//使用465或587端口
props.put("mail.smtp.auth", "true");//设置使用验证
props.put("mail.smtp.starttls.enable", "true");//使用 STARTTLS安全连接
try {
PopupAuthenticator auth = new PopupAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);//打印Debug信息
MimeMessage message = new MimeMessage(session);
Address addressFrom = new InternetAddress(PopupAuthenticator.mailuser + "@qq.com", labelMM);//第一个参数为发送方电子邮箱地址;第二个参数为发送方邮箱地址的标签
Address addressTo = new InternetAddress(acceptAddressMM, acceptlabelMM);//第一个参数为接收方电子邮箱地址;第二个参数为接收方邮箱地址的标签
message.setSubject(subjectMM);
// message.setSubject("发送电子邮件的主题");
// message.setText("发送电子邮件内容");
message.setText(textMM);

message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO, addressTo);
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect("smtp.qq.com", PopupAuthenticator.mailuser, PopupAuthenticator.password);
transport.send(message);
transport.close();
System.out.println("发送成功");
stateMM = "发送成功";
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("发送失败");
stateMM = "发送失败";
}
return stateMM;
}

/**
* 邮件发送完成后,将发送的信息保存数据库
*
* @param labelMM
* @param acceptlabelMM
* @param acceptAddressMM
* @param subjectMM
* @param textMM
* @param stateMM 发送状态
*/
public void saveMailMessage(String labelMM, String acceptlabelMM, String acceptAddressMM, String subjectMM, String textMM, String stateMM) {

}
}

  

技术分享

 

实现发送电子邮件