请教关于javamail的问题
答案:2 悬赏:0 手机版
解决时间 2021-02-19 01:20
- 提问者网友:容嬷嬷拿针来
- 2021-02-18 00:45
请教关于javamail的问题
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-02-18 01:22
什么问题,用法?
package com.peng.sun;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Lession1 {
String host = "";
String user = "";
String password = "";
public void setHost(String host) {
this.host = host;
}
public void setAccount(String user, String password) {
this.user = user;
this.password = password;
}
public void send(String from, String to, String subject, String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host); // 指定SMTP服务器
props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
try {
Session mailSession = Session.getDefaultInstance(props);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from)); // 发件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
message.setSubject(subject); // 邮件主题
message.setText(content); // 邮件内容
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
Lession1 sm = new Lession1();
sm.setHost("smtp.qiye.163.com"); // 指定要使用的邮件服务器
sm.setAccount("sunpeng@cfc-si.com", "*********"); // 指定帐号和密码
sm.send("sunpeng@cfc-si.com", "sunpeng2@cfc-si.com", "标题", "HelloWold!");
}
}
package com.peng.sun;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Lession1 {
String host = "";
String user = "";
String password = "";
public void setHost(String host) {
this.host = host;
}
public void setAccount(String user, String password) {
this.user = user;
this.password = password;
}
public void send(String from, String to, String subject, String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host); // 指定SMTP服务器
props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
try {
Session mailSession = Session.getDefaultInstance(props);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from)); // 发件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
message.setSubject(subject); // 邮件主题
message.setText(content); // 邮件内容
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch(Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
Lession1 sm = new Lession1();
sm.setHost("smtp.qiye.163.com"); // 指定要使用的邮件服务器
sm.setAccount("sunpeng@cfc-si.com", "*********"); // 指定帐号和密码
sm.send("sunpeng@cfc-si.com", "sunpeng2@cfc-si.com", "标题", "HelloWold!");
}
}
全部回答
- 1楼网友:鸠书
- 2021-02-18 02:13
在项目里,后台代码:transport = session.gettransport("smtp");报异常:
javax.mail.nosuchproviderexception: unable to locate provider for protocol: smtp
调试到:session = session.getinstance(props, null);这一步,返回的session实例里 properties 的值是空的.
我试过properties props = new properties();和properties props = system.getproperties();
和session = session.getinstance(props, null);改成session = session.getdefaultinstance(props, null);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯