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: 35252
7
posted
0
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.
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";
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: 35252
7
posted
0
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.