Monday, 14 July 2014

Send Email from Anonymous Id java

Send Email from Anonymous ID java 

Note - Pass your anonymous email id in from parameter of send mail .
Using this function you can also send file as well as have some one in CC

public static void sendEmail(String from ,String to,String cc, String subject, String text) throws IOException 
{
try 
{
Properties properties = new Properties();
String mailSmtpHost = "";
final String username = "";
final String password = ""
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "false");
properties.put("mail.smtp.host", mailSmtpHost);
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties,
 new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
 });
Message emailMessage = new MimeMessage(session);
emailMessage.setFrom(new InternetAddress(from));
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
if(cc!=null && cc!=""){
emailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailMessage.setText(text);

        session.setDebug(true);

Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}


No comments: