go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Any problem? -- Gmail SMTP server losing my FROM address in transition
 
Subject: Any problem? -- Gmail SMTP server losing my FROM address in transition
Author: WebSpider
In response to: Any room to grow? -- holding connection
Posted on: 08/19/2012 04:36:47 PM

For the following settings:

                // authentication
		final String username = "username@gmail.com";
		final String password = "password";
 
		// email information
		String from = "email-from@myFakeCompany.com";
		String to   = "email-to@gmail.com";


On the destination (email-to@gmail.com's Inbox):

        from:       username@gmail.com
          to:       email-to@gmail.com
        date:       Sat, Aug 18, 2012 at 10:53 PM
     subject:       Testing Subject
   mailed-by:       gmail.com


OK, my goal is simple -- I want to use Gmail as the third party SMTP server to send my outgoing emails to my clients. But when the email reaches its destination, my email's FROM address has been lost and it has been replaced by the email account which was used for authentication.

So, I really want to let my clients to get the impression that the messages was sent from FROM address: 'email-from@myFakeCompany.com'. How can I achieve my goal?

Answer: There are 2 solutions:

1) Use paid third party email services by which you can set your 'form' address whatever you want it to be.

2) Use Google Apps (there is a free business account with users less than 10).


 

> On 08/19/2012 03:55:24 PM WebSpider wrote:

Essentially, the sending process
       Transport.send(message);

has done the followings inside:

  • Open a connection/session.
  • Send out message.
  • Close the connection.

    As with all client-server architectures, the establishing connection to SMTP server is always the most costing operation. If you have multiple sending tasks during the life cycle of your application, you have want to keep the connection open for future use.

    Here is the variation:
    
    /**
     *   A simple example to send out email through Google SMTP server via TLS/StatTLS  
     */
    
    package org.example.sendmail;
    
    
    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) {
     
    		// SMTP server information
    		final String host = "smtp.gmail.com";
    		final int port = 487;
    		final String username = "username@gmail.com";
    		final String password = "password";
     
    		// email information
    		String from = "email-from@gmail.com";
    		String to   = "email-to@gmail.com";
    		
    		// Step 1) get the connection by authentication via TLS/StartTLS
    		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);
    			}
    		  }
    		);
     
    		// Step 2) send email 
    		try {
    			Message message = new MimeMessage(session);
    			message.setFrom(new InternetAddress(from));
    			message.setRecipient(Message.RecipientType.TO,
    				new InternetAddress(to));
    			message.setSubject("Testing Subject");
    			message.setText("Testing message");
    
    //			Transport.send(message);
    			
    			message.saveChanges(); // implicit with send()
    			Transport transport = session.getTransport("smtp");
                            transport.connect();
                            transport.sendMessage(message, message.getAllRecipients());
    
                            // close -- you can keep it open for future use if you have other sending task
                            transport.close();
     
     			System.out.println("A message has been sussessfully sent to: '" + to + "'");
     
    		} catch (MessagingException e) {
    			e.printStackTrace();
                            System.out.println("Error in sending email: " + e.toString());
    		}
    	}
    }
    




    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.