gpt4 book ai didi

android - 无法从我的主机发送邮件

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:23 25 4
gpt4 key购买 nike

我正在使用 java mail api 发送邮件。我可以使用 Google 帐户发送邮件,但在使用 SSL 从我的域发送邮件时遇到一些问题。

这是实现,请让我知道我缺少的地方。

// Create all the needed properties
Properties connectionProperties = new Properties();
// SMTP host
connectionProperties.put("mail.smtp.host", "abc.example.com");
// Is authentication enabled
connectionProperties.put("mail.smtp.auth", "true");
// Is StartTLS enabled
connectionProperties.put("mail.smtp.starttls.enable", "true");
// SSL Port
connectionProperties.put("mail.smtp.socketFactory.port", "465");
// SSL Socket Factory class
connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

// SMTP port, the same as SSL port :)
connectionProperties.put("mail.smtp.port", "465");

// Create the session
Session session = Session.getDefaultInstance(connectionProperties, new javax.mail.Authenticator()
{ // Define the authenticator
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("abc@example.net", "abcxdsjdf123");
}
});

System.out.println("done!");

// Create and send the message
try
{
// Create the message
Message message = new MimeMessage(session);
// Set sender
message.setFrom(new InternetAddress("abc@example.net"));
// Set the recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@example.net"));
// Set message subject
message.setSubject("Hello from Test");
// Set message text
message.setText("This is test mail;)");

System.out.print("Sending message...");

// Send the message
Transport.send(message);

System.out.println("done!");
}
catch (MessagingException e)
{
System.out.println(e.toString());
}

下面是我遇到的异常:

javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

任何形式的帮助都将不胜感激。提前致谢。

最佳答案

试试这个..

   public class SentMail {
public boolean deliverMail(String username, String email, String subject, String content) {
boolean sent = false;
try {
String delim = "\n\n";
String text = "Hi ," + delim + content + delim + "Thank you ," + delim + username + "\n" + email;
String from = "from_email";
String pass = "from_pass";
String to = "to_email";
<!--Your host-->
String host = "smtp.zoho.com";
<!--Host port-->
String port = "465";
Properties properties = System.getProperties();
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", port);
properties.put("mail.smtp.startssl.enable", "true");
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(text);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
sent = true;
} catch (Exception e) {

}
return sent;

}
}

关于android - 无法从我的主机发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com