gpt4 book ai didi

java - JAVA发送邮件时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 11:27:07 24 4
gpt4 key购买 nike

我有以下用于发送邮件的代码,但出现以下错误。我在 Windows 7 机器上使用 JAVA 7、Eclipse Luna。我正在使用 smtp.gmail.com 服务器,但每次运行此代码时,我都会在 Transport.send() 方法中收到空指针异常错误。

// Recipient's email ID needs to be mentioned.
String to = "***@tibco.com,***@tibco.com";
String[] mailAddressTo = to.split(",");

// Sender's email ID needs to be mentioned
String from = "PHD";

// Assuming you are sending email from localhost
String host = "10.106.136.29";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// 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));

//can put multiple receivers in the array
InternetAddress[] mailAddress_TO = new InternetAddress [mailAddressTo.length] ;
for(int i=0;i<mailAddressTo.length;i++){
mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]);
}
message.addRecipients(Message.RecipientType.TO, mailAddress_TO);

// Set Subject: header field
message.setSubject("Test Report!");

// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<table border= \'1\' align= \'center\' cellpadding= \'10\' cellspacing= \'10\' frame= \'box\' rules= \'all\' ><caption><h1><em><strong>JUnit Test Report</strong></em></h1></caption><tr><th width=\'200\'>Test ID</th><td>r1014_runtime</td></tr><tr><th>Users</th><td>gajoshi<br><br>dsathiya</td></tr><tr><th> Test Suits Failed</th><td><font color=\'blue\'>10</font></td></tr></table><br><footer>--<br><br><div id=\'mainContainer\' style=\'float:left\'></div></footer><br><br><br>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);

// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("icon/TIB.png");



messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");

// add image to the multipart
multipart.addBodyPart(messageBodyPart);

// put everything together
message.setContent(multipart);
// Send message
try{
Transport.send(message);}
catch(NullPointerException e){
System.out.println(e + " is occured");
}

System.out.println("Sent message successfully....");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

现在我在运行此代码时遇到以下错误。

 Exception in thread "main" java.lang.NullPointerException
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
at javax.mail.Transport.send(Transport.java:123)
at SendMail.main(SendMail.java:111)

请帮助我。

最佳答案

升级您的 JavaMail 版本 1.4.7 或更高版本。此问题已在 Avoid NullPointerException when encountering a bad Content-Type 下修复。 .

关于java - JAVA发送邮件时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30727368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com