zoukankan      html  css  js  c++  java
  • 发送邮件测试

    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

  • 相关阅读:
    Error Correct System(模拟)
    Pasha and String(思维,技巧)
    Vitaliy and Pie(模拟)
    Old Sorting(转化成单调序列的最小次数,置换群思想)
    Segment(技巧 相乘转换成相加 + java)
    Conquering Keokradong && Get the Containers(二分)
    Handshakes(思维) 2016(暴力)
    Dice Notation(模拟)
    “玲珑杯”郑州轻工业学院第八届ACM程序设计大赛暨河南高校邀请赛-正式赛(总结)
    MySQL安装-二进制软件包安装
  • 原文地址:https://www.cnblogs.com/xjyggd/p/3560768.html
Copyright © 2011-2022 走看看