• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2018
  • Last Modified:

java program to send emails

Hello  all,

I want to write a java application that can just send emails to the client.

Do I have to consider what type of mail server does the sender's email belong to?
Do I have to consider what type of mail server (and hence what protocol that mail server uses) does the client's email belong to?

while writing this application.

I went through the documentation of java mail api. And it has all description @ the type of mail server etc.

Pl help.

Thanks
0
at999
Asked:
at999
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
mukundha_expertCommented:
0
 
at999Author Commented:
Thanks mukundh,

in those examples they have used SMTP based mail servers.

What if I want to use mail server like mail.yahoo.com  ?  for example -

String host = "mail.yahoo.com";
String from = "molly_t52@yahoo.com;
String to = "molly_t52@yahoo.com";



Also that code gave error, when I tried to send to my yahoo account.!!
Error is - javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for molly_t52@yahoo.com
     at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1196)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:584)
        at javax.mail.Transport.send0(Transport.java:169)
        at javax.mail.Transport.send(Transport.java:98)
        at emailapplication.Main.main(Main.java:66)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1


'molly_t52@yahoo.com' is a valid address!!!
0
 
at999Author Commented:
Hello All,

I know I am disturbing u guys in the weekend,

but,

I am very confused
The scenario  is - I am trying to connect to send an email yahoo account( without logging into it i mean without providing username/password)  to any mail server account -

wat kind of issues may arise?

do i have to write seperate code for different kind of mail servers?

pl help

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mukundha_expertCommented:
the from Address and to address should be of type InternetAddress.

 InternetAddress from = new InternetAddress(from address);
          InternetAddress[] to = InternetAddress.parse(coma sepearted list of TO Address);

Also you have to validate urself ( like logging in ) first before sending a mail.
0
 
mukundha_expertCommented:
if you are able to configure your yahoo account with your local mail client like outlook, then this code will definitely work
0
 
kaliyugkaarjunCommented:
Hi at999,
Follow these instructions ..
You need to have Javasoft's JavaMail class files which can be downloaded from here http://www.javasoft.com/products/javamail/index.html

You will also need the JavaBeansTM Activation Framework extension or JAF (javax.activation). It is available at http://java.sun.com/beans/glasgow/jaf.html

-----------------------------------------------------------------------------
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

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

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

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

    // set the from and to address
    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);
   

    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
---------------------------------------------------------------

       

Sending Email From Your Application Using Java Mail

By Sudhir Ancha

Now a Day's informing the Clients about the successful updation of data or sending other automated information form your Java Programs, either from your Servlets or from your Applications has become a requirement, more than a feature. Here we show how to use the Java Mail API to send a Mail. To Test Program all you need to have is a SMTP address (Which your ISP Provides).

Before Using this Program, you need to have Javasoft's JavaMail class files which can be downloaded from here http://www.javasoft.com/products/javamail/index.html

You will also need the JavaBeansTM Activation Framework extension or JAF (javax.activation). It is available at http://java.sun.com/beans/glasgow/jaf.html.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

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

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.jcom.net");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(debug);

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

    // set the from and to address
    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);
   

    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}


To Send a Email, from your Program, just call the above method, With the following parameters,

String to -- Email Address of the Recipient

String subject  -- Email Subject

String message -- Content or Body of the Message

String from -- Your (Senders) Email Address

smtp.jcom.net -- Replace this with your ISP's SMTP address.

That's it, you are all set to send a Email.

If your SMTP Server need you to do Authentication using Username and Password before Sending out mail, download the complete code from here :
http://www.javacommerce.com/destination65673/18274/SendMailUsingAuthentication.java
0
 
orhanbabaCommented:
you must use pop3 protocol to send mail if you are not using your own server
look at the sample code below and use a pop3 account such as gmail or your own.
http://www.javaworld.com/javaworld/jw-06-1999/jw-06-javamail-p2.html
0
 
at999Author Commented:
How can I find what protocol to use - POP3 or SMTP ?

Does it depend on the sender's email address or the recipient's email address?

KalyugkaArjun,

I tried to connect to mx4.mail.yahoo.com (sending from my yahoo account, to my own email address)
but I got error that 'Could not connect to SMTP host'
javax.mail.MessagingException:
java.net.ConnectException: Connection timed out: connect
0
 
orhanbabaCommented:
use gmail.
on your gmail account setting there is an option enable pop than use gmail smtp and pop server.
by default pop server uses 110 when you use gmail you have to use it like this
pop.gmail.com:460 (i am not sure about port number just gived an example)
0
 
orhanbabaCommented:
yahoo, hotmail does not support pop3 for free accounts.
0
 
at999Author Commented:
orhanbaba?
this means SENDER's mail shud be GMAIL and not yahoo or hotmail? But receiver's mail can be of any type!!!

But, i can send yahoo to yahoo using  smtp - props.put("mail.smtp.host", host);


but i cannot send from an email server of the type 'IMAP'( e.g. xyz@us.gov etc). The mail server is configured on msexchange and i can access it through msexchange website. But when I try to send mail through java code,
I get error-

java.net.ConnectException: Connection refused: connect

 java.net.ConnectException: Connection refused: connect
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
        at javax.mail.Service.connect(Service.java:275)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at emailmanagement.Main.main(Main.java:73)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
        at java.net.Socket.connect(Socket.java:461)
        at java.net.Socket.connect(Socket.java:411)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
        ... 7 more
 */    


0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> props.put("mail.smtp.host", host);

that is for the server from which you are sending, not to which you are sending.
0
 
at999Author Commented:
ya i can send from yahoo to yahoo by setting following two properties -

mail.smtp.host  = mx4.mail.yahoo.com
mail.smtp.auth = true

I have validator class etc. And yahoo to yahoo(or hotmail) works fine!!!

Now I am tryign to send FROM a Mail server of the type - Microsoft Exchange Server
So I am setting following two properties -
mail.imap.host = mail.myhost.com
mail.imap.auth  = true
I am settign values for IMAP Protocol. Is this method OK for Microsoft Exchange type of server???
But I am getting error message that 'Connection Refused'
Does it mean there is some firewall or some setting on mail server that is stopping this connection?

0
 
at999Author Commented:
How can I find what protocol to use?
SMTP,
POP3 or IMAP?

How can I find exact name of the mail server to set the 'mail.smtp.host' property (or similarly mail.imap.host property)

I checked by sending a simple email using msexchange web version ie. mail.myhost.com/exchange. I sent this sample mail from this website to my yahoo a/c . Once I recd that mail in yahoo, I opend 'Full Headers' To view

        X-Originating-IP:
        Authentication-Results:
        Message-ID:
        etc.

Is this a proper way to find the exact name of the mail server. Like yahoo to yahoo does not work if you set mail.smtp.host to value 'mail.yahoo.com' . You have to set that to 'mx4.mail.yahoo.com'
How can I find these exact values for the mail serverss? is there any technique?


 
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> But I am getting error message that 'Connection Refused'

Are you able to ping to mail.myhost.com? Maybe you should also try specifying the port number.
0
 
mukundha_expertCommented:
if you want to send a mail you should use SMTP.. i think IMAP and POP3 are for receiving mail.

Microsoft exchange server supports SMTP for sending mail
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now