0

Does any one know how to send an email with the desired "from" name in Java ?

I have a code which sends the mail through gmail. Using the smtp settings of gmail, I am able to do that. But, using the same smtp settings, can I send an email from a non-existing mail ID ?

For example: I have a code which sends an email from the existing username (say abc@gmail.com) and the receiver gets the email from abc@gmail.com . But , what I want is , can we send a mail from something like "a@def.com" ? So that, the user receives the mail from "a@def.com" ?

Is that possible ?

AnFi
  • 10,493
  • 3
  • 23
  • 47
N Deepak Prasath
  • 385
  • 2
  • 4
  • 13

3 Answers3

2

While the API may allow you to do that, your difficulty is going to be with the SMTP server's configuration. No sane SMTP server would allow you to control the "from" email address of email messages sent through that SMTP server - that's the first step of making your SMTP server an easy gateway for spammers. Proper SMTP servers (Google's included) will set the "from" email address to be identical to the one you logged in with.

Isaac
  • 16,458
  • 5
  • 57
  • 81
0

You can set it explicitly. But again it depends on the SMTP server that accepts the FROM address. To my knowledge most of the SMTPs are configured to block the sender email ids that do not belong to its domain. If at all they are not configured so, the receiving mail clients may filter the message for not identifying the sender email id from the receiving server and set it as SPAM. This would also cause the SMTP domain being blocked by many other receiving domains.

Hence it is not suggested to be followed.

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
0

You need mail.jar and activation.jar. In my case i implemented using below method

import java.util.HashMap;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.Transport;

class ABC{
......

private void sendSimpleEmail(String to,String subject,String cc,String bcc,String message,HashMap mailMap)
    {

        String smtpHost=mailMap.get("mailhost").toString();
        String smtpPort=mailMap.get("mailport").toString();
        String authrequired=mailMap.get("auth").toString();
        String from=mailMap.get("mailsendfrom").toString();
        String SSLCheck=mailMap.get("sslconfig").toString();

        /* For Authentication */
        String password=mailMap.get("mailpwd").toString();
        String fromUsername=mailMap.get("mailusername").toString();
        /* For Authentication Ends*/
        Properties props = new Properties();
        props.put("mail.smtp.auth", authrequired);
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPort);

        Session session=Session.getDefaultInstance(props);

        Message simpleMessage = new MimeMessage(session);

        try {
            InternetAddress fromAddress = new InternetAddress(from);
            InternetAddress toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            //Exception
        }

        try {

            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(message);

            Transport.send(simpleMessage);          
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
        }                   
    }
.......
}
Prateek
  • 12,014
  • 12
  • 60
  • 81