• 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

Exception while sending email from a Servlet

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a Servlet with to send a email. But i faced a error while sending email. Here is the code:
...
Properties props = new Properties();
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.host","smtp.gmail.com");
props.setProperty("mail.user","abc");
props.setProperty("mail.password","abc");
Session mailSession = Session.getDefaultInstance(props,null);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setContent("This is a test","text/plain");
message.addRecipient(Message.RecipientType.TO,new InternetAddress("abc@yahoo.com"));
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
...

The error i get is : javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first 38sm1046192nzk

Thank you for your help and looking for your replies.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much Annie Smith, let me try your solution and let you know the result later ! thank you again ! cheers !
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annie Smith, I have tried your solution and I have the same error. Any ideas from you ? Thank you in advanced.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex,

Try using, Apache Commons Email 1.0. This works on J2EE 1.4 and upwards. This API is built on top of Sun's Java Mail. It is simpler than Java Mail.

It is as simple as writing and executing the following code.

SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();


Hope this helps.

Regards
Santosh
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Santosh"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Santosh Pasupuleti
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Commons Email jar can be downloaded from http://jakarta.apache.org/site/downloads/downloads_commons-email.cgi.

Examples using the Commons Email jar can be found at http://jakarta.apache.org/commons/email/examples.html.

Best of luck.
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your advices.
I have tried to search throught Internet, I manage to find the code to send Email with SSL. However I encounter problem. Here is the code.
public class SendingEmail {
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailMsgTxt = "Test Message Contents";
private static final String emailSubjectTxt = "A test from gmail";
private static final String emailFromAddress = "from@gmail.com";
private static final String SSL_FACTORY "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = { "to@gmail.com"};

public static void main(String args[]) throws Exception {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

new SendingEmail().sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}

public void sendSSLMessage(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("from@gmail.com", "123456");
}
});

session.setDebug(debug);

Message msg = new MimeMessage(session);
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);
--------------------------------------------------------------------
The error is : Credentials Rejected.
Please help me to solve this problem ! thank you ! forgive me if this thread is not related to Servlets.
 
Annie Smith
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex Marks:
The error is : Credentials Rejected.



I guess you would need to verify if the SMTP_HOST_NAME and SMTP_PORT are correct.
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annie Smith, actually the code above I copy from the Internet and modify, but someone can use it,someone cannot use it,i am the one cannot use ! really dont understand why it cause error !
Hope to get helps from others ! thank you very much !
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Use this code
Properties props = new Properties();
props.put("mail.smtp.host","111.111.111.111");
session = Session.getDefaultInstance(props, null);
message = new MimeMessage(session);
message.setContent(strMessage, "text/html");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(strSubject);
Transport.send(message);


to send email..

In order the above code to work fine, u need to have
mailapi.jar
naming.jar
activation.jar
messagingClient.jar

i am not sure about all of them but let me know if you are facing the problem using the above code
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
you also need to have pop3.jar.

Regards
Makarand Parab
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makarand Parab, would you please more specific about your code? i think i could not get you ! thank you for you help !
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex
I have already worked on the application which sents mail to people in the list which comes from a property file. The code is working fine. I have shared the same code with you. The code requires the ar files as i have stated. What more would u like to know. Ask me and i will let you know

Regards
Makarand Parab
 
Alex Marks
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Makarand Parab, Thank you very much, finally it works. Thank you again and again Cheers.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic