Java mail sender's address displayed rather than his name

JavaJakarta Mail

Java Problem Overview


I am trying to send mail to my friends through my Java Mail application. I am able to do it successfully however the receiver's column in the mailbox shows the complete email address rather than the name of the sender. I tried changing various parameters but still the mailbox would show the full e-mail address rather than the name of the sender.

using this method to send the message:

 public void send(String key){
	String to=key;
	String from="mygmailid";
	String subject="wassp";
	String text="Hello";
	Properties props=new Properties();
	
	props.put("mail.smtp.host", "smtp.gmail.com");
	props.put("mail.smtp.user", "myname");
	props.put("mail.smtp.socketFactory.port", "465");
	props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.port", "465");
	Session mailSession=Session.getDefaultInstance(props);
	Message simpleMessage=new MimeMessage(mailSession);
	
	InternetAddress fromAddress=null;
	InternetAddress toAddress=null;
	
	try{
		fromAddress=new InternetAddress(from);
		toAddress=new InternetAddress(to);
	}
	catch(AddressException e){
		e.printStackTrace();
	}
	
	try{
		simpleMessage.setFrom(fromAddress);
		simpleMessage.setRecipient(RecipientType.TO,toAddress);
		simpleMessage.setSubject(subject);
		simpleMessage.setText(text);
		
		transport.connect("smtp.gmail.com",465, "[email protected]", "mygmailpassword");
		transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
		transport.close();  
		
	}
	catch(MessagingException e){
		e.printStackTrace();
	}
}

I am calling this method as:

public static void main(String[] args) {
	MailSender mailer=new MailSender();
	mailer.send("[email protected]");
}

Java Solutions


Solution 1 - Java

You can set a name in the InternetAddress using

new InternetAddress("[email protected]", "Your Name");

Solution 2 - Java

You should use the two string constructor of InternetAddress to pass in both the e-mail address and the person's name. The resulting e-mail will contain a string like Jarrod indicated.

InternetAddress fromAddress=new InternetAddress("[email protected]", "John Doe");

Solution 3 - Java

	try {

		String from = " EMAIL ID";
		String SMTP_AUTH_PWD = " PASSWORD ";
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.transport.protocol", "smtps");
		props.put("mail.smtps.auth", "true");
		String SMTP_HOST_NAME = "smtp.gmail.com";
		int SMTP_HOST_PORT = 465;
		javax.mail.Session mailSession = Session.getDefaultInstance(props);

		mailSession.setDebug(true);
		Transport transport = ((javax.mail.Session) mailSession)
				.getTransport();

		javax.mail.Message message = new MimeMessage(mailSession);
		message.setSubject("Testing SMTP-SSL");
		message.setContent("", "text/plain");
		message.addRecipient(javax.mail.Message.RecipientType.TO,
				new InternetAddress(receiver));
		transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, from,
				SMTP_AUTH_PWD);
		message.setFrom(new InternetAddress(from," YOUR PREFERED NAME "));
		message.setSubject(subject);
		BodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(body);
		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);
		messageBodyPart = new MimeBodyPart();
		message.setContent(multipart);

		transport.sendMessage(message,
				message.getRecipients(javax.mail.Message.RecipientType.TO));

	}

Solution 4 - Java

How the from field is displayed is a client specific implementation detail.

Usually if the sender is in the form of "Sender Name" <[email protected]> the client will do the correct thing depending on configuration.

Some clients will infer the name information from their address book information if it is missing.

Solution 5 - Java

The answers above are correct but I found I needed to place in a try catch for it to work, here's what I found worked from sendemailwebapp demo application.

Message msg = new MimeMessage(session);

	try {
		msg.setFrom(new InternetAddress(userName, "YourName"));
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
	msg.setRecipients(Message.RecipientType.TO, toAddresses);
	msg.setSubject(subject);
	msg.setSentDate(new Date());
	msg.setText(message);

Solution 6 - Java

Try this code within the try block.You can initialize your name within the setFrom() method of MimeMessage.

simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name"));

ie,

 try{
    simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name"));
    simpleMessage.setRecipient(RecipientType.TO,toAddress);
    simpleMessage.setSubject(subject);
    simpleMessage.setText(text);

    transport.connect("smtp.gmail.com",465, "[email protected]", "mygmailpassword");
    transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
    transport.close();  

 }

Solution 7 - Java

You can force to specify the sender's name by using the greater than and less than Symbol < > as per the following format:

String from="John Smith<[email protected]>";
.
.
.
fromAddress=new InternetAddress(from);

or

public static void main(String[] args) {
    MailSender mailer=new MailSender();
    mailer.send("John Smith<[email protected]>");
}

When receiving the email, the email recipient will see the name "John Smith" in his inbox. (Most email programs shows the name if specified. e.g. Outlook, gmail, hotmail, etc...)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNelo AngeloView Question on Stackoverflow
Solution 1 - JavaRaghavView Answer on Stackoverflow
Solution 2 - JavaSarel BothaView Answer on Stackoverflow
Solution 3 - JavaJohn DavisView Answer on Stackoverflow
Solution 4 - Javauser177800View Answer on Stackoverflow
Solution 5 - JavaDaveView Answer on Stackoverflow
Solution 6 - JavaGayathri RajanView Answer on Stackoverflow
Solution 7 - JavaQA SpecialistView Answer on Stackoverflow