Java 电子邮件程序不发送电子邮件
Java email program not sending emails
我正在为我的 类 之一做一个期末项目,这个程序是为了发送电子邮件到代码中的地址。我知道大部分代码是如何工作的,只是无法理解密码身份验证以及如何连接到 SMTP 服务器和使用特定端口。代码的问题是它在 运行 时不发送电子邮件,并且不给出任何错误消息。任何帮助将非常感激。这是代码。
package application;
import java.util.Properties;
import javax.mail.Message;
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 SendEmail {
public static void main (String [] args) {
String host="smtp.gmail.com";
final String user="myemail@gmail.com";
final String password="password";
String to="targetemail.com";
//imported code
Properties props = new Properties();
props.put("mail.smtp.socketfactory.port", "465");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drink
it.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}
我认为端口值应该如下所示
props.put("mail.smtp.port", "587");
示例配置
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
请尝试下面的代码(修改端口值)。
import java.util.Properties;
import javax.mail.Message;
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 Main {
public static void main(String[] args) {
String host="smtp.gmail.com";
final String user="email@gmail.com";
final String password="*********";
String to="username@gmail.com";
//imported code
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drinkit.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}
我正在为我的 类 之一做一个期末项目,这个程序是为了发送电子邮件到代码中的地址。我知道大部分代码是如何工作的,只是无法理解密码身份验证以及如何连接到 SMTP 服务器和使用特定端口。代码的问题是它在 运行 时不发送电子邮件,并且不给出任何错误消息。任何帮助将非常感激。这是代码。
package application;
import java.util.Properties;
import javax.mail.Message;
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 SendEmail {
public static void main (String [] args) {
String host="smtp.gmail.com";
final String user="myemail@gmail.com";
final String password="password";
String to="targetemail.com";
//imported code
Properties props = new Properties();
props.put("mail.smtp.socketfactory.port", "465");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drink
it.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}
我认为端口值应该如下所示
props.put("mail.smtp.port", "587");
示例配置
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
请尝试下面的代码(修改端口值)。
import java.util.Properties;
import javax.mail.Message;
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 Main {
public static void main(String[] args) {
String host="smtp.gmail.com";
final String user="email@gmail.com";
final String password="*********";
String to="username@gmail.com";
//imported code
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//imported code
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Dwight from the future");
message.setText("At 8:00, someone poisons the coffee. Do NOT drinkit.");
Transport.send(message);
System.out.println("message sent!");
}
catch (MessagingException mex)
{
System.out.println("Error: unable to send message....");
mex.printStackTrace();
}
}
}