这是使用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);
}
}
这使用MyGmail帐户发送电子邮件
我正在测试中从gmail帐户向另一个人发送电子邮件
但是,当我收到电子邮件时,它很好......但是,现在显示了发件人的全名
而不是“Jatin Shah”(我的名字)......它显示了bhatin.shah(我的用户名)