8

How do I change the setFrom() method to whatever I want? I can send e-mails through my gmail accoutn and change the setFrom text, but it shows my username for the email. I have tried using my yahoo account as well and I get an authentication error.

I want to change the from address. The code is as follows:

import java.util.Properties;

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

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "username@gmail.com";
        final String password = "password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            }
        );

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to-email@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
shaunw
  • 360
  • 1
  • 3
  • 10
  • Dont use Google's relay? – Perception Dec 01 '12 at 00:24
  • 2
    My friend, you are using Googles relay to send your mail (`props.put("mail.smtp.host", "smtp.gmail.com");`). Use another relay if you want less strict enforcing of what username displays as the from address. – Perception Dec 01 '12 at 00:32
  • 2
    What you're trying to do is commonly called "spam". :-) That's why Gmail won't let you do it. Assuming you have a legitimate reason for doing this, see this Gmail help page: [Sending mail from a different address](https://support.google.com/mail/bin/answer.py?hl=en&answer=22370) – Bill Shannon Dec 01 '12 at 00:36
  • were you looking for `setReplyTo()`? – ThePCWizard Dec 05 '12 at 06:13

4 Answers4

1

Faced Same problem while using "smtp.gmail.com". Use Mandrill,it will work. Once you setup a Mandrill account,use "smtp.mandrillapp.com" on port 587. For Authentication,set username=your mandrill username and password=API key generated in your account.

0

Many reputable SMTP relays will not allow you to fake your identity (that would make it even easier for spammers to abuse the service). That said, if your goal is to avoid more mail in your inbox, Google allows you to modify your email address so that you could filter any replies to your email.

jdigital
  • 11,926
  • 4
  • 34
  • 51
0

(Assuming I understand your goal and it's not to send spam) The Apache Commons library we use does it by calling setPersonal on the InternetAddress. http://docs.oracle.com/javaee/7/api/javax/mail/internet/InternetAddress.html#setPersonal-java.lang.String-java.lang.String-

        Message message = new MimeMessage(session);
        InternetAddress me = new InternetAddress("from-email@gmail.com");
        me.setPersonal("My name");
        message.setFrom(me);
coladict
  • 4,799
  • 1
  • 16
  • 27
-3

Here is the full code You can use the following code. This is working Fine for me

class SendMail {
public static void main(String[] args) {
    System.out.println("In side main()---------------------");

    Properties props = new Properties();


    props.put("mail.smtp.host", "hostname");
    props.put("mail.smtp.port", "port");//

    props.put("mail.smtp.socketFactory.port", "port");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");



    props.put("mail.smtp.auth", "true");

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

                protected PasswordAuthentication getPasswordAuthentication() {
                    System.out
                            .println("In side getPasswordAuthentication------------------And Before returning PasswordAuthentication");
                    return new PasswordAuthentication("from@gmail.com",
                            "password");

                }

            });
    System.out
            .println("mail and password has been sent********************");
    try {
        System.out
                .println("we are  in try{} block.................................");
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear User," + "\n\n This is testing only!");

        Transport.send(message);

        System.out
                .println("Mail has been sent successfully..........................");

    } catch (MessagingException e) {
        System.out.println("we are in catch block...................");
        e.printStackTrace();

    }
}

}

raj
  • 1
  • 1