以下是我发送邮件的代码:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public void sendMail(String m_from,String m_to,String m_subject,String m_body){
try {
Session m_Session;
Message m_simpleMessage;
InternetAddress m_fromAddress;
InternetAddress m_toAddress;
Properties m_properties;
m_properties = new Properties();
m_properties.put("mail.smtp.host", "usdc2spam2.slingmedia.com");
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
m_properties.put("mail.smtp.auth", "true");
m_properties.put("mail.smtp.port", "9000");
m_Session=Session.getDefaultInstance(m_properties,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("aaaaa","bbbbb@1"); // username and the password
}
});
m_simpleMessage = new MimeMessage(m_Session);
m_fromAddress = new InternetAddress(m_from);
m_toAddress = new InternetAddress(m_to);
m_simpleMessage.setFrom(m_fromAddress);
m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
m_simpleMessage.setSubject(m_subject);
m_simpleMessage.setContent(m_body, "text/html");
//m_simpleMessage.setContent(m_body,"text/plain");
Transport.send(m_simpleMessage);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SendMail send_mail = new SendMail();
String empName = "xxxxx";
String title ="<b>Hi !"+empName+"</b>";
send_mail.sendMail("123erft@slingmedia.com", "abz@gmail.com", "Please apply for leave for the following dates", title+"<br>by<br><b>HR<b>");
}
}
但是当我运行代码时,它给了我以下异常。
javax.mail.MessagingException: Could not connect to SMTP host: usdc2spam2.slingmedia.com, port: 9000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at samples.SendMail.sendMail(SendMail.java:46)
at samples.SendMail.main(SendMail.java:55)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
当我ping到usdc2spam2.slingmedia.com
它时,它给我的答复没有任何问题。我在用windows 7
请帮我解决这个问题。
这是这两行让我感到困扰的问题:
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
并添加了这一行:
m_properties.put("mail.smtp.starttls.enable", "true");
删除并添加以上代码行后,它可以正常工作。
这是我用来发送电子邮件的代码: javax.mail.MessagingException:无法连接到SMTP主机:SMTP.gmail.com,端口:465; 嵌套异常为: java.net.connectException:连接被拒绝 在com.sun.mail.SMTP.smtpransport.openserver(smtpransport.java:1961) 在com.sun.mail
我在windows server 2008 R2上遇到以下错误,并且在我的防火墙入站规则上启用了: 请救命!!
我正在获取javax.mail.MessagingException:无法连接到SMTP主机:主机名端口:25响应:552 有时邮件发送成功。但有时我会遇到例外。 我不清楚为什么会发生这种情况。如果有什么事情出了问题,那么就不应该发送邮件。但例外情况有时还是会发生。 javax.mail.messagingException:无法连接到SMTP主机:mail.mydomain.com,端口:25,
SMTP错误:无法连接到SMTP主机。无法发送消息。 邮件程序错误:SMTP错误:无法连接到SMTP主机。 我似乎找不到让PHPMailer在CentOS下工作的方法。邮件在XAMPP的Windows下工作正常,但我总是在Linux下遇到这个错误。 SMTP服务器是一个在端口25上侦听的Lotus Domino,CentOS机器根本没有防火墙,奇怪的是,即使是mail()也不能工作。它不返回任何内
} 谁能告诉我,我的代码是不是错了,或者我遗漏了什么?以下是完整的错误输出。
我正在尝试从我的java发送电子邮件,但我无法连接到我的主机。这里是我的代码: 这里是我的代码: 这里又是我的报告错误: 有人能帮帮我吗?:(