这是Gmail smtp的示例:
import java.io.*;import java.net.InetAddress;import java.util.Properties;import java.util.Date;import javax.mail.*;import javax.mail.internet.*;import com.sun.mail.smtp.*;public class Distribution { public static void main(String args[]) throws Exception { Properties props = System.getProperties(); props.put("mail.smtps.host","smtp.gmail.com"); props.put("mail.smtps.auth","true"); Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("mail@tovare.com"));; msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("tov.are.jacobsen@iss.no", false)); msg.setSubject("Heisann "+System.currentTimeMillis()); msg.setText("Med vennlig hilsennTov Are Jacobsen"); msg.setHeader("X-Mailer", "Tov Are's program"); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport)session.getTransport("smtps"); t.connect("smtp.gmail.com", "admin@tovare.com", "<insert password here>"); t.sendMessage(msg, msg.getAllRecipients()); System.out.println("Response: " + t.getLastServerResponse()); t.close(); }}
现在,仅当您希望将项目依赖关系降至最低时,才可以这样做,否则我可以热烈推荐使用apache中的类
http://commons.apache.org/email/
托雷·雅各布森
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)