使用java应用程序发送电子邮件
为什么这个代码不能发送电子邮件?没有错误,只是没有发送使用java应用程序发送电子邮件,java,email,Java,Email,为什么这个代码不能发送电子邮件?没有错误,只是没有发送 package tips.mails; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import j
package tips.mails;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
private String from;
private String to;
private String subject;
private String text;
public SendMail(String from, String to, String subject, String text){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
}
public void send(){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
Transport.send(simpleMessage);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[])
{
new SendMail("source", "dist","Subject", "Test Message!!!");
}
}
您实例化了一个
SendMail
对象,但对它不做任何操作。也许你也应该执行你的send()
方法。你实例化了一个SendMail
对象,却什么也不做。也许你也应该执行你的send()
方法。你忘了调用send。试试这个
new SendMail("source", "dist","Subject", "Test Message!!!").send();
你忘了给send打电话了。试试这个
new SendMail("source", "dist","Subject", "Test Message!!!").send();
您是否实际调用过
send()
?如果没有异常,邮件服务器上会发生什么?你调试过吗?将其指向您可以控制的邮件服务器(类似于Smtp4Dev或类似的东西),并查看邮件服务器日志。也许服务器拒绝了它?也许收件人正在将其标记为垃圾邮件?如果问题的本质是“我运行了这段代码,但我的收件箱没有收到新邮件”,那么在这段时间内可能会出现很多问题,其中大多数问题与代码无关。你的send()方法的作用是什么??。。这不是意味着inkoved吗???:你是否真的调用过send()
?如果没有例外,邮件服务器上会发生什么?你调试过吗?将其指向您可以控制的邮件服务器(类似于Smtp4Dev或类似的东西),并查看邮件服务器日志。也许服务器拒绝了它?也许收件人正在将其标记为垃圾邮件?如果问题的本质是“我运行了这段代码,但我的收件箱没有收到新邮件”,那么在这段时间内可能会出现很多问题,其中大多数问题与代码无关。你的send()方法的作用是什么??。。它不意味着inkoved吗???:Pjavax.mail.Messagine异常:无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应:-1在com.sun.mail.SMTP.SMTPTransport.openServer(SMTPTransport.java:1270)在com.sun.mail.SMTP.SMTPTransport.protocolConnect(SMTPTransport.java:370)在javax.mail.Service.connect(Service.java:275)上javax.mail.Service.connect(Service.java:156)javax.mail.Service.connect(Service.java:105)javax.mail.Transport.send0(Transport.java:168)javax.mail.Transport.send(Transport.java:98)tips.mail.SendMail.send(SendMail.java:53)tips.mail.SendMail.main(SendMail.java:63)它只在带有端口25的me.com服务器上运行如何使它在所有服务器上运行但现在它没有运行,错误:530 5.7.0没有发出STARTTLS命令。javax.mail.MessagingException:无法连接到SMTP主机:SMTP.gmail.com,端口:465,响应:-1 at com.sun.mail.SMTPTransport.openServer(SMTPTransport.java:1270)com.sun.mail.SMTPTransport.protocolConnect(SMTPTransport.java:370)javax.mail.Service.connect(Service.java:275)javax.mail.Service.connect(Service.java:156)javax.mail.Service.connect(Service.java:105)javax.mail.Transport.send0(Transport.java:168)javax.mail.Transport.send(Transport.java:98)在tips.mails.SendMail.send(SendMail.java:53)在tips.mails.SendMail.main(SendMail.java:63)上,它只在带有端口25的me.com服务器上运行。如何使它在所有服务器上运行?但现在它不运行,错误:530 5.7.0没有发出STARTTLS命令。