The method setContent(Multipart) in the type MimeMessage is not applicable for the arguments (MimeMultipart)

liangxiao 发布于 2012/04/05 12:51
阅读 1K+
收藏 0

下面是我发送邮件的源代码:

        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();

 

加载中
0
liangxiao
liangxiao

如何我将

            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.*;

 

 

 

0
liangxiao
liangxiao

如果将:

             MimeMultipart mp=new MimeMultipart();
           
            MimeBodyPart mdp = new MimeBodyPart();

 

改成:

 

            Multipart mp=new MimeMultipart();
           
            BodyPart mdp = new MimeBodyPart();

 

报错:

 

Type mismatch: cannot convert from MimeMultipart to Multipart

 

返回顶部
顶部