This is a sample function to send an email using Gmail
public void sendEmail(String from, ArrayList<String> to, String subject,
String content, boolean contentIsHtml) throws EmailException
{
try
{
Properties props = new Properties();
props.put("mail.transport.protocol", EmailConfig.getGmailTransportProtocol());
props.put("mail.smtp.host", EmailConfig.getGmailSMTPHost());
props.put("mail.smtp.socketFactory.port", EmailConfig.getGmailSMTPPort());
props.put("mail.smtp.socketFactory.class", EmailConfig.getSocketFactoryClass());
props.put("mail.smtp.auth", EmailConfig.getGmailAuthRequired());
props.put("mail.smtp.port", EmailConfig.getGmailSMTPPort());
SMTPAuthenticator authenticator = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, authenticator);
//session.setDebug(true);
Transport transport = session.getTransport();
MimeMessage message = new MimeMessage(session);
message.setSubject(subject);
message.setSender(EmailUtil.getInternetAddress(
EmailConfig.getGmailUsername(), "jatin Shah"));
message.setRecipients(
RecipientType.TO,
EmailUtil.getInternetAddresses(to));
if(contentIsHtml)
message.setContent(content, "text/html");
else
message.setContent(content, "text/plain");
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
System.out.println("Email Sent!!");
}
catch(Exception e)
{
e.printStackTrace();
throw new EmailException(e);
}
}
This sends an email using MyGmail account
I am sending an email from gmail account to another in my test
However, when I receive an email it is fine … however, the full name of the sender is now shown
Instead of “Jatin Shah” (my name) … it shows bhatin.shah (My Username)
First, read this FAQ entry of common mistakes.
To fix your problem, use setFrom instead of setSender.