本教程将帮助您使用Java从本地SMTP服务器发送电子邮件。本教程使用javax.mail(JavaMail)API用于发送电子邮件
c:> set classpath=mail.jar;.;Linux:
# export JAVA_HOME=/opt/jdk1.8.0_05/ # export PATH=$PATH:$JAVA_HOME/bin # export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar:mail.jar:.
import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMailJavaAPI { public static void main(String[] args) throws Exception{ String to="recipient@example.com"; String from="sender@example.com"; Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); String msgBody = "Sending email using JavaMail API…"; try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from, "NoReply")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, "Mr. Recipient")); msg.setSubject("Welcome To Java Mail API"); msg.setText(msgBody); Transport.send(msg); System.out.println("Email sent successfully…"); } catch (AddressException e) { throw new RuntimeException(e); } catch (MessagingException e) { throw new RuntimeException(e); } } }
# javac SendMailJavaAPI.java # java SendMailJavaAPI
关注云架构公众号
Linux入门
QQ交流群:308781113