Java 当我尝试使用调度程序自动发送邮件时,对默认会话的访问被拒绝
我需要在特定的时间间隔内自动将电子邮件发送至我的公司电子邮件帐户。但是,即使我没有使用默认实例,我也得到了对默认会话的Java 当我尝试使用调度程序自动发送邮件时,对默认会话的访问被拒绝,java,session,jakarta-mail,Java,Session,Jakarta Mail,我需要在特定的时间间隔内自动将电子邮件发送至我的公司电子邮件帐户。但是,即使我没有使用默认实例,我也得到了对默认会话的访问被拒绝的异常。不需要身份验证。请帮助我解决此异常并定期发送邮件 MailServer.java public class MailServer extends javax.mail.Authenticator { private String mailhost ="host"; //"smtp.mail.yahoo.com"; //"smtp.mail.c
访问被拒绝的异常。不需要身份验证。请帮助我解决此异常并定期发送邮件
MailServer.java
public class MailServer extends javax.mail.Authenticator
{
private String mailhost ="host"; //"smtp.mail.yahoo.com"; //"smtp.mail.com";
final String username = "username";
final String password = "password";
private Session session;
public MailServer(String user, String password) {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "host");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, username);
}
});
session = Session.getInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(password, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public class Testing extends TimerTask
{
public void run()
{
MailServer sender = new MailServer(Constants.setFrom, Constants.setPassword);
try {
sender.sendMail("Subject","This is Java4s",Constants.setFrom,Constants.emailTO);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Email Sent Succesfully...");
}
}
.properties文件
setFrom = fgf.er@xxx.com
setPassword=xxx
#repeat for every 10 seconds
timetoquery = 10s
#start after 2seconds for first time..
delay = 2s
emailTO = fgh.gm@xxx.com,fgh.hg@xxx.com
Testing.java
public class MailServer extends javax.mail.Authenticator
{
private String mailhost ="host"; //"smtp.mail.yahoo.com"; //"smtp.mail.com";
final String username = "username";
final String password = "password";
private Session session;
public MailServer(String user, String password) {
Properties props = new Properties();
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "host");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, username);
}
});
session = Session.getInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(password, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public class Testing extends TimerTask
{
public void run()
{
MailServer sender = new MailServer(Constants.setFrom, Constants.setPassword);
try {
sender.sendMail("Subject","This is Java4s",Constants.setFrom,Constants.emailTO);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Email Sent Succesfully...");
}
}
控制台
Scheduler started..
java.lang.SecurityException: Access to default session denied
at javax.mail.Session.getDefaultInstance(Session.java:292)
at javax.mail.Transport.send0(Transport.java:141)
at javax.mail.Transport.send(Transport.java:80)
at java4s.MailServer.sendMail(MailServer.java:63)
at java4s.Testing.run(Testing.java:15)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Email Sent Succesfully...
问题是您正在隐藏一个变量:
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, username);
}
});
session = Session.getInstance(props, this); // this is the local variable not the instance one
因此,在sendMail
中:
MimeMessage message = new MimeMessage(session);
会话为null
,java mail尝试使用默认会话。我认为这是一个输入错误,在测试类中,您正在创建Gmail服务器类的对象,当您发布邮件服务器类的代码时,它是一个输入错误。在我的IDE中,我已经给出了正确的类名。如果您使用的是gmail,那么prop是错误的否我使用的是带有端口25的Outlook server。电子邮件将以指定的电子邮件ID发送。现在的问题是,新电子邮件警报和收件箱中都没有显示发件人电子邮件ID。“发件人邮件ID”在“收件箱”列表中为空,但在邮件打开时会显示。对于我来说,听起来像是一个全新的问题,请随时为新问题发布另一个问题。