这里是通过QQ邮箱 sina新浪邮箱 163邮箱的简单发送Email
1.首先去官網下載發送Email的相應的jar文件如 smtp.jar mailapi.jar等
2.去QQ? sina 163中的設置里開啟POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV? 一些你發送Email的服務歇協議? 及生成授權碼等
3.具體的代碼實例
/*
?? ? * ServerHost:發送郵件的服務器
?? ? * ServerPort:發送郵件服務器的端口
?? ? * userName:登陸郵件發送服務器的用戶名
?? ? * userPwd:登陸郵件發送服務器的密碼(QQ郵箱是通過授權碼)
?? ? * fromAddress:發送者的郵箱地址
?? ? * toAddress:接收郵箱者的郵箱地址
?? ? */ssss
public static void SendEmails(String ServerHost,String ServerPort,final String userName,final String userPwd, String fromAddress,String toAddress) throws Exception{
?? ??? ?Properties per=new Properties();
?? ??? ?per.put("mail.smtp.host",ServerHost);
?? ??? ?per.put("mail.smtp.port",ServerPort);
?? ??? ?per.put("mail.smtp.auth",true);// 是否需要身份驗證
?? ???? Session session=Session.getDefaultInstance(per,new Authenticator() {
?? ??? ??? ?protected PasswordAuthentication getPasswordAuthentication() {
?? ??? ??? ??? ?return new PasswordAuthentication(userName, userPwd);
?? ??? ??? ?}
?? ??? ?});
?? ???? Message msg=new MimeMessage(session);
?? ??? ?msg.setFrom(new InternetAddress(fromAddress));
?? ??? ?msg.setRecipient(Message.RecipientType.TO,new InternetAddress(toAddress));
?? ??? ?msg.setSubject("測試專題");
?? ??? ?msg.setSentDate(new Date());
?? ??? ?msg.setText("這是我發送的內容");
?? ??? ?Transport.send(msg);
?? ?}
public static void main(String[] args) throws Exception {
?? ??? ?//SendEmails("smtp.qq.com","587","aaa@qq.com","vhwilqtqcvfgjhjg","aaa@qq.com","bbb@qq.com");
?? ??? ?//SendEmails("smtp.163.com","25","ccc@163.com","aabbcc","ccc@163.com","qqqq@163.com");
?? ???? //SendEmails("smtp.sina.com","25","dddd@sina.cn","llkkjj","www@sina.cn","qwert@sina.cn");
?????? //這樣就可以發送一封簡單的郵件了?? 也可以通過自己公司內部的郵箱服務器填寫相應的端口和服務器也是可以的
??? }?
總結
以上是生活随笔為你收集整理的这里是通过QQ邮箱 sina新浪邮箱 163邮箱的简单发送Email的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文献管理软件简介
- 下一篇: 【TOOLS】python3利用SMTP