惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
小众软件
小众软件
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
博客园_首页
IT之家
IT之家
S
SegmentFault 最新的问题
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
月光博客
月光博客
有赞技术团队
有赞技术团队
H
Help Net Security
云风的 BLOG
云风的 BLOG

博客园 - xjy

elasticsearch 6.1.1 transport jar docker-compose 获取时区 maven插件 jquery_ui_api中文 sax xpath读取xml字符串 Ant打包 [转]html5音乐播放器 javax mail网址 vbscript调用fso INNO SETUP 读取可变注册表路径的问题 fso查找被删除的文件 NTP校时设置 Tomcat启动,不能加载项目问题。 生成javadoc 【转】关闭特定虚拟机上声音嘟嘟声 vs2010取消显示所有文件 崩溃 FastJson wps使用技巧
发送邮件测试
xjy · 2014-02-22 · via 博客园 - xjy

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class mailTest {
public mailTest() {
}

public static void main(String[] args) {
send();
System.out.println("====");
}

public static void sendGmail() {
SimpleEmail email = new SimpleEmail();
email.setTLS(true);
email.setSmtpPort(587);
email.setSslSmtpPort("587");
email.setHostName("smtp.gmail.com");
email.setAuthentication("xx@gmail.com", "xx"); // 用户名和密码
try {
email.addTo("xx@21cn.com"); // 接收方
email.setFrom("xx@gmail.com"); // 发送方
email.setSubject("Java Mail Test"); // 标题
email.setMsg("Just a simple send test ."); // 内容
System.out.println("start send");
email.send();
}
catch (EmailException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}

public static void sendGmailSSL()
{

String to="xx@xx.com";//change accordingly

//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xx@gmail.com", "xxxxx");//change accordingly
}
});

//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("xx@gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");

//send message
Transport.send(message);

System.out.println("message sent successfully");

} catch (MessagingException e) {throw new RuntimeException(e);}

}
}


public static void send() {
SimpleEmail email = new SimpleEmail();
email.setSmtpPort(25);
email.setHostName("smtp.21cn.com");
email.setAuthentication("xx@21cn.com", "xx"); // 用户名和密码
try {
email.addTo("xx@gmail.com"); // 接收方
email.setFrom("xx@21cn.com"); // 发送方
email.setSubject("Java Mail Test"); // 标题
email.setMsg("Just a simple send test ."); // 内容
System.out.println("start send");
email.send();
}
catch (EmailException e) {
e.printStackTrace();
}
}
}

package play.utils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.X509Certificate;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

// SSL Sockets created by this factory won't check if certificates are signed with a root certificate (or chained from root)
public class YesSSLSocketFactory extends SSLSocketFactory {

public static class YesTrustManager implements X509TrustManager {

public void checkClientTrusted(X509Certificate[] cert, String authType) {
}

public void checkServerTrusted(X509Certificate[] cert, String authType) {
}

public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
private SSLSocketFactory factory;

public YesSSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{new YesTrustManager()}, null);
factory = sslcontext.getSocketFactory();
} catch (Exception ex) {
}
}

public static SocketFactory getDefault() {
return new YesSSLSocketFactory();
}

public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
return factory.createSocket(socket, s, i, flag);
}

public Socket createSocket() throws IOException {
return factory.createSocket();
}

public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
}

public Socket createSocket(InetAddress inaddr, int i) throws IOException {
return factory.createSocket(inaddr, i);
}

public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
return factory.createSocket(s, i, inaddr, j);
}

public Socket createSocket(String s, int i) throws IOException {
return factory.createSocket(s, i);
}

public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}

public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}

jira:   SSLV3

 nested exception is:

    javax.net.ssl.SSLHandshakeException: Server chose unsupported or disabled protocol: SSLv3

javax.mail.MessagingException: Connect failed

Cause

The javax.mail library used by JIRA to retrieve mail messages from POP and IMAP servers does not support the SSLv3 protocol.

Resolution

The easiest way to resolve this is to enable SSLv3 by adding a startup option and restarting.

  1. For pop3/s, you would add the following option:

    -Dmail.pop3s.ssl.protocols=SSLv3

  2. For imaps, you would add the following option:

    -Dmail.imaps.ssl.protocols=SSLv3

Alternately, you can:

  1. configure your mail server to allow SSLv2 or TLS, both of which are supported.
  2. allow JIRA to use the non-SSL ports when checking mail.
  3. set up an stunnel connection to the mail server, and allowing JIRA to connect to the mail server via the tunnel.

参考:http://blog.csdn.net/xiaojiang0829/article/details/17276871