Avatar of a1programmer
a1programmer
Flag for United States of America asked on

SMTP from Java and Gmail

I'm working on a Java POP3 / SMTP client for sending/receiving emails using which will be integrated with a custom socket server.

The problem is that when I send the email from my PC, the emails don't get received by the recipient, because the destination email thinks it's spoofed.  

I figured that if I sent an email from GMail / SMTP, while authenticated, etc, that I would get the same result as if I logged into the GMail web interface... But, this is what the message looks like.

Return-Path: <verify@domain_dot_com>
Received: from Desktop (ip-yyy-zzz-xx.dhcp.ispdomain.com [ip.yyy.zzz.xx])
        by mx.google.com with ESMTPS id 21sm74440iwn.11.2010.02.18.13.05.13
        (version=SSLv3 cipher=RC4-MD5);
        Thu, 18 Feb 2010 13:05:14 -0800 (PST)
Date: Thu, 18 Feb 2010 16:05:05 -0500 (EST)
From: verify@domain_dot_com
To: target@target_dot_net
Message-ID: <16627215.0.166211352034541.JavaMail.UserName@Desktop>
Subject: Device Verified
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

message body


Here's a section of the code: (See snippet)



sendEmail(String from,String to, String subject, String body) throws MessagingException {
       
            String host = "smtp.gmail.com";
        
            Properties props = new Properties();
            
            props.put("mail.smtps.auth", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "465");
            
            Session sendSession = Session.getInstance(props);
            sendSession.setDebug(true);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());

//          // Set message content
            msg.setText(body);
            
            Transport t = sendSession.getTransport("smtps");
            try {
                t.connect(host, username, password);
                t.sendMessage(msg, msg.getAllRecipients());
            } finally {
                t.close();
            }
        
            System.out.println("Email Sent.");
     
    }

Open in new window

Java EEJava

Avatar of undefined
Last Comment
a1programmer

8/22/2022 - Mon
CEHJ

Mick Barry

a1programmer

ASKER
I'm not having problems connecting/authenticating to the gmail server.  The problem is with my desktop being listed as the sender.

Nonetheless, I added the starttls.enabled= true (which isn't actually needed)...  Yet I still get the same results.

The first link that "objects" posted shows the same result in the debug output.
DEBUG: setDebug: JavaMail version 1.4.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 mx.google.com ESMTP 12sm3177596pwj.7
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465

EHLO Desktop
250-mx.google.com at your service, [xx.yy.zzz.ip]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 
AUTH LOGIN
334 VXNlcm5hbWU6
dmVyaWZ5QGNhbWVycmlmaWMuY29t
334 UGFzc3dvcmQ6
JHVwZXJjYW1lcmE=
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<verify@domain.com>
250 2.1.0 OK 12sm3177596pwj.7
RCPT TO:<to_Address@.....net>
250 2.1.5 OK 12sm3177596pwj.7
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   to_address@....net
DATA
354  Go ahead 12sm3177596pwj.7
Date: Thu, 18 Feb 2010 19:58:00 -0500 (EST)
From: verify@domain.com
To: to_address@.....net
Message-ID: <21636312.0.1253143083915.JavaMail.Username@Desktop>
Subject: Device Verified
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

verifyTEST00
.
250 2.0.0 OK 1266541090 12sm3177596pwj.7
QUIT
221 2.0.0 closing connection 12sm3177596pwj.7

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
CEHJ

>>MimeMessage msg = new MimeMessage(session);

is not the Session you create with your Properties
a1programmer

ASKER
Hmmm. Still getting the same results.

Even though you couldn't see it, I had a "session" that had pretty much the same settings, it just also had pop3 settings.  

I've tried 3 different ways, and the mails still seem like they come from my desktop.
CEHJ

I have no problem with your code after the change i mentioned
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
a1programmer

ASKER
There isn't really a problem with my code, per se.  

Does it appear as if it were sent from your desktop?  What is displayed in the headers of the sent message? Also, what is displayed in the debug output?
CEHJ

Well it displays my hostname in the headers, together with the IP address - just as expected. What parts of the debug output are you interested in?
ASKER CERTIFIED SOLUTION
a1programmer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mick Barry

> There isn't really a problem with my code, per se.  

correct, your code is fine
you can change the headers if you want, but no need to
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
a1programmer

ASKER
Still looking into how to close this question, etc.