我正在创建一个向随机用户发送电子邮件的项目。
有一个单独的函数可以调用sendemail方法。
我在做什么错了?
package com.java.quartz.job;
import java.util.List;
import java.util.Properties;
import javax.mail.*;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail
{
private static final String SMTP_HOST_NAME = "mail.yandex.com.au";
private static final String SMTP_AUTH_USER = "no-reply@abc.com.au";
private static final String SMTP_AUTH_PWD = "abc";
private static String cmpname= "",replyto="";
public static int tolength=0;
public static int bcclength=0;
public static int cclength=0;
public static void sendmail2(List <String> receiver,List<String> cclist,List<String> bcclist ,String message,String Subject,String from)
{
try {
System.out.println("Sending Mail from ::"+from+ "to :: "+receiver);
postMail2(receiver,cclist,bcclist,message,Subject, from);
System.out.println("Mail sent successfully");
} catch (Exception e) {
System.out.println("Exception in SendBookingMailRequest - - -" +
e.getMessage());
}
}
public static void postMail2(List <String> recipient,List<String> cclist,List<String> bcclist, String subject, String message, String from)
{
try {
System.out.println("NEW111q1");
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", "25");
props.put("mail.debug", "false");
props.put("mail.smtp.starttls.enable", "true");
System.out.println("to 1 :: ");
Authenticator auth= new SMTPAuthenticator();
System.out.println("to 2 :: props "+props+ " auth :: "+auth );
Session session1 = Session.getDefaultInstance(props, auth);
System.out.println("to:: 3"+session1 );
---- No logs after this ----
MimeMessage msg = new MimeMessage(session1);
System.out.println("to:: 4"+msg);
InternetAddress addressFrom = new InternetAddress(SMTP_AUTH_USER,cmpname);
System.out.println("to:: 5"+addressFrom);
InternetAddress[] addresses = InternetAddress.parse(replyto);
System.out.println("to:: 6"+addresses);
msg.setReplyTo(addresses);
System.out.println("to:: " + recipient);
msg.setFrom(addressFrom);
System.out.println("send by :: "+cmpname);
msg.setFrom(new InternetAddress(cmpname));
String strrecipient = String.join(",", recipient);
String ccstrrecipient=String.join(",", cclist);
String bccstrrecipient=String.join(",", bcclist);
msg.addRecipients(Message.RecipientType.TO, strrecipient);
msg.addRecipients(Message.RecipientType.CC, ccstrrecipient);
msg.addRecipients(Message.RecipientType.BCC, bccstrrecipient);
Address Toarr[] = msg.getRecipients(Message.RecipientType.TO);
Address bccarr[]=msg.getRecipients(Message.RecipientType.BCC);
Address ccarr[]=msg.getRecipients(Message.RecipientType.CC);
tolength=Toarr.length;
bcclength=bccarr.length;
cclength=ccarr.length;
System.out.println("tolength:: "+tolength+"bcclength :: "+bcclength+" cclength:: "+cclength);
msg.setSubject(subject);
msg.setContent(message, "text/html");
System.out.println("Message :: "+msg);
//Transport.send(msg);
} catch (Exception ex) {
System.out.println("Error While Authenticating For Sending MAil ::" +
ex.getMessage());
ex.printStackTrace();
}
}
private static class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getpasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
我在此处在此代码中创建cclist
和bcclist
,并且此列表由另一个代码发送。 System.out.println("to:: 3"+session1 );
在系统上运行此代码时,即使没有任何错误。
以下是上述代码的日志:
从:: info@cbdmovers.co.nz发送邮件到:: [abc@gmail.com]
NEW111q1
到1 ::
到2 ::道具{mail.transport.protocol = smtp,mail.debug = false,mail.smtp.port = 25,mail.smtp.auth = true,mail.smtp.starttls.enable = true,mail.smtp。 host = mail.yandex.com.au} auth :: com.java.quartz.job.SendEmail$SMTPAuthenticator@1b6124f1
到:: 3javax.mail.Session@3d9b30bf