java EE 5配置邮件发送 qq企业邮箱

java EE 5配置QQ企业邮件发送

1.在项目的WebRoot/META-INF/新建context.xml 具体内容如下:
<Context>
    <Resource name="mail/Session"
    auth="Container"
    type="javax.mail.Session"
    mail.smtp.host="localhost"
    mail.smtp.auth="true"
    mail.transport.protocol="smtp" />
</Context>

2.讲javamail-1.4下的mail.jar拷贝到Tomcat/lib下
3.实例代码如下:

try {
    Context initCtx = new InitialContext();
    Context envCtx = (Context)initCtx.lookup("java:comp/env");
    Session session = (Session)envCtx.lookup("mail/Session");
    
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(usname)); //发件人账号
    InternetAddress[] to = new InternetAddress[1];
    to[0] = new InternetAddress(email);
    message.setRecipients(Message.RecipientType.TO, to);
    message.setSubject("邮件主题"); //邮件主题
    message.setText("邮件正文 Lol你值得拥有"); //邮件正文文本
    
    Transport transport = session.getTransport();
    transport.connect("smtp.exmail.qq.com",usname,uspwd); //发件的smtp  发件人账号  发件人密码
    transport.sendMessage(message,to);
    transport.close();
} catch (AddressException e) {
    e.printStackTrace();
} catch (NamingException e) {
    e.printStackTrace();
} catch (MessagingException e) {
    e.printStackTrace();
}

=================================================

java EE 5配置QQ邮件发送

1.在项目的WebRoot/META-INF/新建context.xml 具体内容如下:
<Context>
    <Resource name="mail/Session"
    auth="Container"
    type="javax.mail.Session"
    mail.smtp.host="localhost"
    mail.smtp.auth="true"
    mail.transport.protocol="smtp"
    mail.smtp.ssl.enable="true"
    mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
    mail.smtp.socketFactory.fallback="false"
    mail.smtp.port="465"
    mail.smtp.socketFactory.port="465" />
</Context>

2.讲javamail-1.4下的mail.jar拷贝到Tomcat/lib下
3.实例代码如下:

String femail = "发件人邮箱地址";
String fpwd = "授权码";
String[] toEmail = new String[]{femail};
String subject = "注册验证";
String text = "欢迎您注册,您的验证码是1234";
String smtp = "smtp.qq.com";
try {
    
    Context initCtx = new InitialContext();
    Context envCtx = (Context)initCtx.lookup("java:comp/env");
    Session session = (Session)envCtx.lookup("mail/Session");
    session.setDebug(true);
    URLName urlName = new URLName("");
    session.setPasswordAuthentication(urlName, new PasswordAuthentication(femail, fpwd));
    
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(femail)); //发件人邮箱
    InternetAddress[] to = new InternetAddress[toEmail.length];
    for(int i=0;i<toEmail.length;i++)
    {
        to[i] = new InternetAddress(toEmail[i]); //收件人邮箱
    }
    message.setRecipients(Message.RecipientType.TO, to);
    message.setSubject(subject);
    message.setText(text);
    
    Transport transport = session.getTransport();
    transport.connect(smtp,465,femail,fpwd); //发件账号密码 465:SSL端口
    transport.sendMessage(message,to);
    transport.close();
} catch (AddressException e) {
    e.printStackTrace();
} catch (NamingException e) {
    e.printStackTrace();
} catch (MessagingException e) {
    e.printStackTrace();
}

你可能感兴趣的:(java,mail.jar,ee,qq企业邮箱,5配置邮件发送)