我想让我的Android应用程序使用我的邮件服务器发送邮件,我的服务器使用端口25,所以我的应用程序需要在不出现ssl的情况下发送邮件。我尝试过这里大多数类似问题中提到的方法,但没有一个有效,因此我提出了一个新问题。以下是我读到的一些链接:
在android中使用JavaMail API发送电子邮件,使用smtp但不使用SSL
通过smtp发送电子邮件,使用TSL
Java Mail:SSLHandshakeException在端口25上发送电子邮件时不使用SSL
JavaMail:javax。网ssl。例外:太阳。安全验证器。使用带TLS的JavaMail从VPS发送邮件时出现异常
http://java.sun.com/products/javamail/javamail-1.4.2/SSLNOTES142.txt
下面是我的邮件发送类:
public class MailSender extends Authenticator {
private String user;
private String password;
private String [] to;
private String from;
private String port;
private String sport;
private String host;
private String subject;
private String body;
private boolean auth;
private boolean debuggable;
private Multipart multi;
public MailSender(){
host = "me.myserver.com";
port = "25";
sport = "25";
user = "";
password = "";
from = "";
subject = "";
body = "";
debuggable = true;
auth = true;
multi = new MimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
public MailSender(String user, String password){
this();
this.user = user;
this.password = password;
}
public boolean send() throws Exception {
Properties props = setProperties();
try{
Session session = Session.getInstance(props, this);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo = new InternetAddress[to.length];
for(int i=0; i<to.length; i++){
addressTo[i] = new InternetAddress(to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
multi.addBodyPart(messageBodyPart);
msg.setContent(multi);
Transport transport = session.getTransport("smtps");
transport.connect(host, 25, user, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multi.addBodyPart(messageBodyPart);
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
private Properties setProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", host);
if(debuggable) {
props.put("mail.debug", "true");
}
if(auth) {
props.put("mail.smtp.auth", "true");
}
//props.put("mail.smtp.ssl.enable", "false");
//props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", sport);
//props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//props.put("mail.smtp.socketFactory.fallback", "true");
props.setProperty("mail.smtp.ssl.enable", "true");
props.setProperty("mail.smtp.ssl.socketFactory.class",
"DummySSLSocketFactory");
props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");
//props.put("mail.smtp.starttls.enable", "false");
return props;
}
public void setTo(String[] toAddress) {
this.to = toAddress;
}
public void setFrom(String fromAddress) {
this.from = fromAddress;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setBody(String body) {
this.body = body;
}
}
DummySSLSocketFactory。爪哇:
public class DummySSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;
public DummySSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null,
new TrustManager[] { new DummyTrustManager()},
null);
factory = (SSLSocketFactory)sslcontext.getSocketFactory();
} catch(Exception ex) {
// ignore
}
}
public static SocketFactory getDefault() {
return new DummySSLSocketFactory();
}
public Socket createSocket() throws IOException {
return factory.createSocket();
}
public Socket createSocket(Socket socket, String s, int i, boolean flag)
throws IOException {
return factory.createSocket(socket, s, i, flag);
}
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();
}
}
DummyTrustManager.java:
public class DummyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] cert, String authType) {
// everything is trusted
}
public void checkServerTrusted(X509Certificate[] cert, String authType) {
// everything is trusted
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
日志中的错误消息:
08-15 16:08:59.338: W/System.err(16163): javax.mail.MessagingException: Could not connect to SMTP host: me.myserver.com, port: 25;
08-15 16:08:59.338: W/System.err(16163): nested exception is:
08-15 16:08:59.338: W/System.err(16163): javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.338: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.338: W/System.err(16163): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
08-15 16:08:59.338: W/System.err(16163): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
08-15 16:08:59.346: W/System.err(16163): at javax.mail.Service.connect(Service.java:288)
08-15 16:08:59.346: W/System.err(16163): at Android.sender.MailSender.send(MailSender.java:100)
08-15 16:08:59.346: W/System.err(16163): at Android.sender.Sender$SendMail.doInBackground(Sender.java:73)
08-15 16:08:59.346: W/System.err(16163): at Android.sender.Sender$SendMail.doInBackground(Sender.java:1)
08-15 16:08:59.354: W/System.err(16163): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 16:08:59.354: W/System.err(16163): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 16:08:59.354: W/System.err(16163): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 16:08:59.354: W/System.err(16163): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 16:08:59.354: W/System.err(16163): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 16:08:59.354: W/System.err(16163): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 16:08:59.354: W/System.err(16163): at java.lang.Thread.run(Thread.java:856)
08-15 16:08:59.354: W/System.err(16163): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.354: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.362: W/System.err(16163): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
08-15 16:08:59.362: W/System.err(16163): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
08-15 16:08:59.362: W/System.err(16163): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
08-15 16:08:59.362: W/System.err(16163): at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
08-15 16:08:59.362: W/System.err(16163): at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366)
08-15 16:08:59.362: W/System.err(16163): ... 12 more
08-15 16:08:59.362: W/System.err(16163): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5bfaf480: Failure in SSL library, usually a protocol error
08-15 16:08:59.362: W/System.err(16163): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x580b87de:0x00000000)
08-15 16:08:59.362: W/System.err(16163): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
08-15 16:08:59.362: W/System.err(16163): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
08-15 16:08:59.362: W/System.err(16163): ... 16 more
在这里,我已经将“传输传输=session.get传输(“smtps”);”改为“传输传输=session.get传输(“smtps”);”,错误日志中的消息已经更改。
08-15 17:18:13.986: W/System.err(21399): javax.mail.MessagingException: Could not convert socket to TLS;
08-15 17:18:13.986: W/System.err(21399): nested exception is:
08-15 17:18:13.986: W/System.err(21399): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399): at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1339)
08-15 17:18:13.986: W/System.err(21399): at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:420)
08-15 17:18:13.986: W/System.err(21399): at javax.mail.Service.connect(Service.java:288)
08-15 17:18:13.986: W/System.err(21399): at Android.sender.MailSender.send(MailSender.java:100)
08-15 17:18:13.986: W/System.err(21399): at Android.sender.Sender$SendMail.doInBackground(Sender.java:73)
08-15 17:18:13.986: W/System.err(21399): at Android.sender.Sender$SendMail.doInBackground(Sender.java:1)
08-15 17:18:13.986: W/System.err(21399): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-15 17:18:13.986: W/System.err(21399): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-15 17:18:13.986: W/System.err(21399): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-15 17:18:13.986: W/System.err(21399): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-15 17:18:13.986: W/System.err(21399): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-15 17:18:13.986: W/System.err(21399): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-15 17:18:13.986: W/System.err(21399): at java.lang.Thread.run(Thread.java:856)
08-15 17:18:13.986: W/System.err(21399): Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:398)
08-15 17:18:13.986: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:647)
08-15 17:18:13.986: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:618)
08-15 17:18:13.986: W/System.err(21399): at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
08-15 17:18:13.986: W/System.err(21399): at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1336)
08-15 17:18:13.986: W/System.err(21399): ... 12 more
08-15 17:18:13.986: W/System.err(21399): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.986: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:192)
08-15 17:18:13.994: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:163)
08-15 17:18:13.994: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:597)
08-15 17:18:13.994: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
08-15 17:18:13.994: W/System.err(21399): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
08-15 17:18:13.994: W/System.err(21399): ... 16 more
08-15 17:18:13.994: W/System.err(21399): Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
08-15 17:18:13.994: W/System.err(21399): ... 21 more
这看起来像是您的另一个线程的重复:使用JavaMail应用编程接口的SSL异常,尽管在那里看起来您确实想使用SSL。
如果不想使用SSL,为什么要使用DummySlssocketFactory和DummyStrustManager?完全摆脱它们,使用“smtp”协议,而不是“smtps”协议。而且,正如我在您的另一个线程中解释的,您根本不需要任何套接字工厂属性,即使您使用的是SSL。
您没有说明是否要使用STARTTLS,即是否要使用纯文本(非SSL)连接到邮件服务器,然后将连接切换到SSL。如果要这样做,请将mail.smtp.starttls.enable属性设置为true。
我有一个IMAP服务器主机名:xyz.com.我的SMTP主机名是:comcast.net. 现在,当我通过Outlook发送电子邮件时,它会自动将我的ID[发件人ID]显示为:myusername@xyz.com而不是myusername@comcast.net. 我想用JavaMail API实现同样的目标。 当我使用JavaMail API时,它将发送者显示为myusername@comca
我下载了Office 365 Connect ASP。NET MVC示例代码http://dev.office.com/code-samples-detail/5985,将其注册为Azure Active Directory中的应用程序(我具有全局管理员权限),并授予其所需的权限-“登录并读取用户配置文件”和“以用户身份发送邮件”(无其他权限),以及配置了web。配置为“ClientID”和“Cl
我试图从域发送邮件,但得到一些错误。
问题是,当我更改Gateway和Transformmer方法签名以accomodate Multipart file和MailMessageDto时,我将在transform方法中将其转换为MimeMessageHelper对象,spring integration无法理解transform方法。 我想知道控制器方法的签名应该是什么,以及如何在XML中声明Transformer transform
本文向大家介绍python3使用QQ邮箱发送邮件,包括了python3使用QQ邮箱发送邮件的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python3使用QQ邮箱发邮件的具体代码,供大家参考,具体内容如下 直接上代码 如果登录失败可能是你的qq邮箱没有启动SMTP 解决: 启动后QQ会提供授权码,将代码中密码改为授权码即可。 成功后: 以上就是本文的全部内容,希望对大家的学习有所帮
问题内容: 如何使用curl命令行程序从gmail帐户发送电子邮件? 我尝试了以下方法: 使用file.txt作为电子邮件的内容,但是,当我运行此命令时,出现以下错误: 是否可以从仍由curl托管的个人服务器托管帐户发送电子邮件?这样会使身份验证过程更容易吗? 问题答案: curl –url 'smtps://smtp.gmail.com:465’ –ssl-reqd \ –mail-from '