The method setContent(Multipart) in the type MimeMessage is not applicable for the arguments (MimeMultipart)
下面是我发送邮件的源代码:
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String emailContent = request.getParameter("emailContent");
try
{
Properties props = new Properties();
Session session = Session.getInstance(props);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
MimeMultipart mp=new MimeMultipart();
MimeBodyPart mdp = new MimeBodyPart();
mdp.setContent(emailContent,"text/html;charset=GBK");
mp.addBodyPart(mdp);
message.setContent(mp); //这里报错了,错误信息如题,求问这是怎么回事啊?坐等
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host,from, password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将:
MimeMultipart mp=new MimeMultipart();
MimeBodyPart mdp = new MimeBodyPart();
改成:
Multipart mp=new MimeMultipart();
BodyPart mdp = new MimeBodyPart();
报错:
Type mismatch: cannot convert from MimeMultipart to Multipart
如何我将
MimeMultipart mp=new MimeMultipart();
MimeBodyPart mdp = new MimeBodyPart();
改成:
Multipart mp=new MimeMultipart();
BodyPart mdp = new MimeBodyPart();
这一句话就不报错了:message.setContent(mp); 但是上面那两句就报错了,求问,是不是我的jar或者什么引用的不对啊?
下面是我的jar 引用:
import java.io.IOException;
import java.io.PrintWriter;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart;
import java.util.*;
import java.net.*;