Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why java.mail not able to send out email?

User_YWIZ9Nov 25 2022

Heres the code:
public class SendEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "xxx";

// Sender's email ID needs to be mentioned
String from = "service@domain.us";
final String username = "service";//change accordingly
final String password = "xxxxxx";//change accordingly

// Assuming you are sending email through relay.jangosmtp.net
String host = "mail.privateemail.com";

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.connectiontimeout", 8000);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");//587 for tls 465 for ssl
System.setProperty("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

 // Create a default MimeMessage object.  
 Message message = new MimeMessage(session);  

 // Set From: header field of the header.  
 message.setFrom(new InternetAddress(from));  

 // Set To: header field of the header.  
 message.setRecipients(Message.RecipientType.TO,  
  InternetAddress.parse(to));  

 // Set Subject: header field  
 message.setSubject("Testing Subject");  

 // 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 = "\<H1>Dear Sir or Madam:\</H1>" +  

"<span>We are a mobile marketing company, help you generate leads on mobile devices with Pull Mobile content and Push Mobile coupons/deals, grow your businesses locally, national and worldwide." +
"We've a MMS platform to reach customers on the go with data-driven campagins and targeted MMS ads to achieve better ROI. Call or visit: <a href='http://upstik.com'>Upstik.us</a> It's easy to set up a time with our free consultation to better assist you.</span>" +
"<img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);

 // second part (the image)  
 messageBodyPart = new MimeBodyPart();  
 DataSource fds = new FileDataSource(  
  "upstik-service.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  
 Transport.send(message);  

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

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
mail.privateemail.com is fine, why it hangs?

Comments
Post Details
Added on Nov 25 2022
0 comments
472 views