wood burning stoves*
The moose likes Other JSE/JEE APIs and the fly likes Need Help in creating a Java Mailer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Need Help in creating a Java Mailer" Watch "Need Help in creating a Java Mailer" New topic
Author

Need Help in creating a Java Mailer

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
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.


Ping & DNS - my free Android networking tools app
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi,

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

Thanks,
dinesh.
Abhijith Prabhakar
Ranch Hand

Joined: Dec 29, 2006
Posts: 56
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

Joined: Oct 12, 2006
Posts: 134
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
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help in creating a Java Mailer
 
Similar Threads
japanese character as junk
simple javamail example getting error
can not run simple javamail sample
Give me solution to send mail
Send Mail Error