Java Mail入门实例

需要的jar包:mailapi.jar、stmp.jar。

import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Demo { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getInstance(props); session.setDebug(true); //生成邮件相关信息 Message msg = new MimeMessage(session); msg.setText("邮件内容"); msg.setSubject("邮件主题"); msg.setFrom(new InternetAddress("[email protected]")); Transport transport = session.getTransport(); transport.connect("smtp.stmpserver.com", 25, "username", "password"); //发送邮件到指定地址的邮箱 transport.sendMessage(msg, new Address[]{new InternetAddress("[email protected]"), new InternetAddress("[email protected]")}); transport.close(); } }

你可能感兴趣的:(java,exception,session,properties,String,import)