progulj
progulj

Reputation: 25

Wrong sender mail address

Hi I'm trying to send email, but i constantly get wrong sender email address, it is basically the same address of the account I'm receiving mail. In code and debug looks like I'm sending address OK, but when I receive mail it comes with wrong address. I've tried with several different accounts and all had same problem. Any clue what I am missing or doing wrong?

This is my code for sending email:

void sendEmail(Email email) throws Exception {

    final String to = email.getEmailTo();

    final String name = email.getName();

    final String lastName = email.getLastName();

    final String from = email.getEmailFrom();

    final String password = email.getPassword();

    // Assuming you are sending email from smtp.gmail.com
    String host = "smtp.gmail.com";

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

    Session session = Session.getInstance(properties, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(to, password);
        }

    });

    session.setDebug(true);

    try {

        InternetAddress fromAddress = new InternetAddress(from, name + " "
                + lastName);

        Message message = new MimeMessage(session);
        message.setFrom(fromAddress);
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject(email.getSubject());
        message.setText(email.getMessage() + " " + from);
        Transport.send(message);
    } catch (MessagingException messageException) {
        throw new RuntimeException(messageException);
    }catch (Exception exception) {
        throw new RuntimeException(exception);
    }

Debug:

235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 2.1.0 OK dj7sm7618504wjb.3 - gsmtp
RCPT TO:<[email protected]>
250 2.1.5 OK dj7sm7618504wjb.3 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354  Go ahead dj7sm7618504wjb.3 - gsmtp
From: Email Sender <[email protected]>
To: [email protected]
Message-ID: <1979522159.01432382909940.JavaMail.Pero@Pero-PC>
Subject: Hi
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Is it working?  [email protected].

250 2.0.0 OK 1432382910 dj7sm7618504wjb.3 - gsmtp
QUIT
221 2.0.0 closing connection dj7sm7618504wjb.3 - gsmtp

Upvotes: 0

Views: 1540

Answers (1)

RealSkeptic
RealSkeptic

Reputation: 34628

You are using Google's SMTP server for sending this message.

When you do that, Google's rules apply. It basically puts your Gmail account as the sender, unless you have set up the alternative address in advance in your Gmail account, and it's an address you own.

The reason for this is that spammers and propagators of malware use this technique of sending mail that pretends to be from somebody else, and Google does not want to become a party to this sort of activity. So they limit the from address to whatever is set up in advance and has been verified to belong to you.

You can see the information on how to set up alternative "from" addresses on your Gmail account here.

Another option is to use a different SMTP server that does not have this kind of limitation, but setting up an SMTP server that will be trusted by all recipient servers is not a trivial task - it's easy to get blacklisted if you don't have various verification records in your domain server etc. It's almost always best to rely on the SMTP server of some reliable service provider, but if you do that, you have to follow their rules, and they may also have similar rules regarding source address.

Upvotes: 1

Related Questions