我使用以下Java代码发送电子邮件。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
String to = "abcd@gmail.com";
String from = "web@gmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to))
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
当我运行该文件时,我收到以下错误:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
如果有人能帮助我,我真的很感激。
如何解决ConnectException
?
答案 0 :(得分:5)
我可以看到您正在尝试使用Gmail
作为SMTP服务器。这一行不正确:
properties.setProperty("smtp.gmail.com", host);
您正在使用主机名作为属性名称,这是不正确的。因为您没有设置mail.smtp.host
属性JavaMail尝试连接到'localhost'。改为设置以下属性:
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.user", "username"); // User name
properties.put("mail.smtp.password", "password"); // password
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
答案 1 :(得分:0)
将主机名更改为:smtp.gmail.com
String host = "localhost";
将其更改为:
String host = "smtp.gmail.com"
答案 2 :(得分:0)
你必须尝试这样的事情
String host = "smtp.gmail.com";
String from = "user name";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "asdfgh");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] to_address = new InternetAddress[to.length];
int i = 0;
// To get the array of addresses
while (to[i] != null) {
to_address[i] = new InternetAddress(to[i]);
i++;
}
System.out.println(Message.RecipientType.TO);
i = 0;
while (to_address[i] != null) {
message.addRecipient(Message.RecipientType.TO, to_address[i]);
i++;
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
// alternately, to send HTML mail:
// message.setContent("<p>Welcome to JavaMail</p>", "text/html");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
目前您正在尝试连接 localhost
礼貌{{3p>
答案 3 :(得分:0)
尝试将此添加到您的属性:
private static final String SMTP_HOST = "smtp.gmail.com"; //Use Gmail's SMTP Host.
private static final String SMTP_PORT = ""; //Use Gmail's Port Number for SMTP.
properties.setProperty("mail.smtp.host", SMTP_HOST);
properties.setProperty("mail.smtp.port", SMTP_PORT);
答案 4 :(得分:0)
假设我的
邮箱:liferayasif@gmail.com
密码:密码
public Session getSession() {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "liferayasif"); // User name
props.put("mail.smtp.password", "password"); // password
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.session.mail.pop3.host", "pop.gmail.com");
props.put("mail.session.mail.pop3.password", "password");
props.put("mail.session.mail.pop3.port", "110");
props.put(" mail.session.mail.pop3.user", "USER");
props.put("mail.session.mail.imap.host", "imap.gmail.com");
props.put("mail.session.mail.imap.port", "993");
props.put("mail.session.mail.store.protocol", "imap");
props.put("mail.session.mail.transport.protocol", "smtp");
props.put("mail.session.mail.smtp.host", "smtp.gmail.com");
props.put("mail.session.mail.smtp.password", "password");
props.put("mail.session.mail.smtp.user", "liferayasif@gmail.com");
props.put("mail.session.mail.smtp.port", "465");
props.put("mail.session.mail.smtp.auth", "true");
props.put("mail.session.mail.smtp.starttls.enable", "true");
props.put("mail.session.mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("liferayasif", "password");
}
});
return session;
}