發送郵件測試

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

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();
}
}
apache

 

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"); session

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);}

}
}socket


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();
}
}
}ui

 

 

 

 

package play.utils;this

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

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

// 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 {rest

public static class YesTrustManager implements X509TrustManager {code

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

相關文章
相關標籤/搜索