Question

I want to send one mail to specified recipient but my sender can have different mail account like outlook or ymail or gmail. Is it possible to send an email from different email id to same recipient? I'm using this code :

try
    {final  String from=request.getParameter("from");String smtpServ="",port="";final String pass=request.getParameter("pass");String to=request.getParameter("to");String subject=request.getParameter("subject");String body=request.getParameter("msg");if(from.contains("@gmail.com")){smtpServ="smtp.gmail.com";port="465";}else if(from.contains("@outlook.com") || from.contains("@hotmail.com")){smtpServ="smtp.live.com";port="587";}else if(from.contains("@ymail.com") || from.contains("@yahoo.com") || from.contains("@rocketmail.com") || from.contains("@yahoo.in")){smtpServ="smtp.mail.yahoo.com";port="465";}Properties props = System.getProperties();
          // -- Attaching to default Session, or we could start a new one --

    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.host",smtpServ);        
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", port);

        Session session1 = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from,pass);
            }
        });Message message = new MimeMessage(session1);message.setFrom(new InternetAddress(from));             
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

    message.setSubject(subject);
     message.setText(body);
          // -- Set some other header information --

    message.setHeader("MyMail", "Java Mail Test");
     message.setSentDate(new Date());Transport.send(message);System.out.println("Message sent to"+to+" OK."); }
    catch (Exception ex)
    { ex.printStackTrace();System.out.println("Exception "+ex); }
Was it helpful?

Solution

Sending mails via Java (using Java Mail API) requires you to setup an SMTP host that would send your email to the recipient after proper log-in validation. I've used Java Mail with Gmail IDs and Gmail as SMTP host server. Some e-mail service providers do not allow sending emails from outside of their own web-services (making them incompatible with Java Mail).

But, in order to support multiple senders, you'll need to do the following:

  1. Change the SMTP server to your correct host as per the email ID in use.
  2. Run the Authenticator, with the new e-mail address and password, every time the sender ID is changed.
  3. Then send your message.

My suggestion would be to store these sender email IDs and passwords (and SMTP hosts) when the program is initially run and then iterate the above steps with required changes, for each email id and password pair.

The Caveat:

In my opinion it's best to stick with a single sender else, multiple messages with similar content, by varied sender addresses, may be marked down as Spam by the recipients email service provider.

OTHER TIPS

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

replace this thing with,

message.setRecipients(Message.RecipientType.TO, Address[] addresses);

[N.B.- addresses are ur desired IDs.]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top