• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

javamail.providers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i am using javamail API to send mails, my code is executed but the mails are goes to badmail folder of the host.

my code is below.

package com;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* @author gaucho
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SendMailUsingAuthentication {

private static final String SMTP_HOST_NAME = "192.168.2.33";
private static final String SMTP_AUTH_USER = "chiranjeevi@theikos.com";
private static final String SMTP_AUTH_PWD = "Password1";
private static final String emailMsgTxt = "Please visit my project at ";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "chiranjeevi@theikos.com";

// Add List of Email address to who email needs to be sent to
private static final String[] emailList = { "chiranjeevi@theikos.com","v.chiranjeevi@inbox.com","eswarprasad.v@gmail.com"};

public static void main(String args[]) throws Exception {
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt,
emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}

public void postMail(String recipients[], String subject, String message,
String from) throws MessagingException {
boolean debug = false;
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

//Set the host smtp address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug","true");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}

/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {

public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic