现在大多邮箱支持客户端登陆时,需要使用激活码进行验证。
package javamail;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
//开启POP3/SMTP服务,授权码:??????????
public class SendMailMessage {
public static void main(String[] arg) throws MessagingException, GeneralSecurityException{
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
// 设置环境信息
Session session = Session.getInstance(props);
// 创建邮件对象
Message msg = new MimeMessage(session);
msg.setSubject("JavaMail测试");
// 设置邮件内容
msg.setText("这是一封由JavaMail发送的邮件!这是新的测试内容");
// 设置发件人
msg.setFrom(new InternetAddress("-----------@qq.com"));
Transport transport = session.getTransport();
// 连接邮件服务器 (发件人账号和密码),现在用这种方式发送邮件需要把密码改成授权码,需要去邮箱界面开启开启POP3/SMTP服务,获取授权码
transport.connect("smtp.qq.com", 465,"你的邮箱账号", "你的授权码");
// 发送邮件
transport.sendMessage(msg,
new Address[] {new InternetAddress("java_mail_002@163.com")});
// 关闭连接
transport.close();
}
}
如果发生��ʹ����Ȩ���¼�������뿴: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256错误,说明你没有使用授权码登陆