我使用以下代码发送邮件,但我得到例外。
javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25,响应:421
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SimpleEmailClient {
public static void main(String[] args) {
try {
// Recipient's email ID needs to be mentioned.
String to = "rajasekharb974@gmail.com";
String cc="raj479.mits@gmail.com";
// Sender's email ID needs to be mentioned
String from ="brajasekharreddy@chn.aithent.com";
String host ="localhost";
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
message.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc));
// Set Subject: header field
message.setSubject("Email Notification");
// set the actual message
message.setText("Hi,\n\nAcknowledgement Message :: \n\n"+"\n\nThanks,\n"+"Raj");
// Send message
Transport.send(message, message.getAllRecipients());
System.out.println("Sent message successfully....");
} catch (Exception e) {
System.out.println(e);
}
}
}*
答案 0 :(得分:0)
您可以尝试这样: -
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String [] args)
{
// Sender's email ID needs to be mentioned
String from = "your_mail@gmail.com";
String pass ="mail_pass";
// Recipient's email ID needs to be mentioned.
String to = "reciver_mail@gmail.com";
String host = "smtp.gmail.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}