• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Need Help in creating a Java Mailer

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to send a mail from my Java Program.
The Code i used is:
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendApp {
public static void send(String smtpHost, int smtpPort,
String from, String to,
String subject, String content)
throws AddressException, MessagingException {
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", ""+smtpPort);
Session session = Session.getDefaultInstance(props, null);

// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(content);

// Send the message
Transport.send(msg);
}

public static void main(String[] args) throws Exception {
// Send a test message
send("smtp.mail.yahoo.com", 25, "sivadinesh7@gmail.com", "sivadinesh7@yahoo.co.in",
"re: dinner", "How about at 7?");
}
}

I am getting the exception "UnknownHostException".

Can anybody tell me a valid SMTP host so that i can fix this problem.

Thanks in Advance!!!
dinesh.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you ping that host? (I can.)

I'm certain that you also need to provide username and password in your code. No mail server lets just anybody connect and send mails through it these days.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could you please explain how to provide username and password? Is there any API for that?

Thanks,
dinesh.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use authenticator for the same. I have given the complete method used by me. Hope this helps

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

/* To use this program, change values for the following three constants,

SMTP_HOST_NAME -- Has your SMTP Host Name
SMTP_AUTH_USER -- Has your SMTP Authentication UserName
SMTP_AUTH_PWD -- Has your SMTP Authentication Password

Next change values for fields

emailMsgTxt -- Message Text for the Email
emailSubjectTxt -- Subject for email
emailFromAddress -- Email Address whose name will appears as "from" address

Next change value for "emailList".
This String array has List of all Email Addresses to Email Email needs to be sent to.

*/


private static final String SMTP_HOST_NAME = "smtphostname";
private static final String SMTP_AUTH_USER = "test123";
private static final String SMTP_AUTH_PWD = "password";
private static final String emailMsgTxt = "Your request for resources has been placed. Delivery operations team will work on this & get back to you.\n\nRegards\nDOM team";
private static final String emailSubjectTxt = "Your request has been placed";
private static final String emailFromAddress = "xxxz@xxx.com";

public void SendMailUsingAuthentication(Context context){
Session session = null;
boolean debug = false;
String[] emailList = {context.getEmp_Mail_ID()};
try{
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");

javax.mail.Authenticator auth = new SMTPAuthenticator();
try{
session = Session.getInstance(props, auth);
session.setDebug(debug);
}
catch(Exception expSession){
System.out.println("***********************exceptoin in try first****************");
expSession.printStackTrace();
}


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

// set the From, TO and CC address
InternetAddress addressFrom = new InternetAddress(emailFromAddress);
InternetAddress[] addressTo = new InternetAddress[emailList.length];
for (int i = 0; i < emailList.length; i++)
{
addressTo[i] = new InternetAddress(emailList[i]);
}

msg.setFrom(addressFrom);
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(emailSubjectTxt);
msg.setContent(emailMsgTxt, "text/plain");

System.out.println("***********************B4 send****************");
try{
Transport.send(msg);
System.out.println("********************** Mail Sent****************");
}
catch(Exception e){
e.printStackTrace();
}
}catch(Exception ex) {
ex.printStackTrace();
System.out.println("EXCEPTION############");
}
}
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abijith,

Thanks for your suggestions. I am getting the UnknownHostException while trying to use the smtp.mail.yahoo.com. So if it is possible please share some mail host names.

Thanks,
dinesh.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So if it is possible please share some mail host names.


What's wrong with using the mail server of your ISP? These days, nobody is going to let you use their mail server without you being properly identified.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic