java邮件开发遇到no mimebodypart content

java邮件开发遇到no mimebodypart content

这里写的是一个复杂的邮件,里面包含附件和图片(注意看红色的字体)
源代码:

public static void main(String[] args) {
//Properties:配置文件,
Properties properties = new Properties();
//设置QQ邮箱服务器进行ssl验证  
properties.setProperty("mail.smtp.ssl.enable", "true");
//QQ邮箱发送的协议为stmp    
properties.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(properties);
session.setDebug(true);
//Message:创建和解析邮件内容
Message msg = new MimeMessage(session);
try {
    //设置发送者的信息
    msg.setFrom(new InternetAddress("392016423@qq.com"));
    //设置显示邮件的标题名称为‘你最尊敬的大佬’
    msg.setFrom(new InternetAddress("\""+ MimeUtility.encodeText("你最尊敬的大佬") +"\"<392016423@qq.com> "));
} catch (MessagingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
//指明整个邮件的架构,包括正文和附件
MimeMultipart msgMultipart = new MimeMultipart("mixed");
try {
       //构造整个邮件的架构
    msg.setContent(msgMultipart);
    MimeBodyPart attch1 = new MimeBodyPart();  //邮件的附件
    MimeBodyPart attch2 = new MimeBodyPart();  //邮件的附件
    MimeBodyPart content = new MimeBodyPart(); //邮件的正文
****//注意在这里必须设置每一个MimeBodyPart 类对象的context,不然就会报no mimebodypart content****
attch1.setContent("", "text/html;charset=UTF-8");
attch2.setContent("", "text/html;charset=UTF-8");
 //将各个组成部分组合在一起
 msgMultipart.addBodyPart(attch1);
 msgMultipart.addBodyPart(attch2);
 msgMultipart.addBodyPart(content);

 //对邮件的附件进行细节处理
 DataSource ds1 = new FileDataSource("");
 DataHandler dh1 = new DataHandler(ds1);
 attch1.setDataHandler(dh1);
 try {
         //对附件进行设置中文名字:用MimeUtility类的encodeText(String str)方法
      attch2.setFileName( MimeUtility.encodeText(""));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 DataSource ds2 = new FileDataSource("");
 DataHandler dh2 = new DataHandler(ds2);
 attch1.setDataHandler(dh2); //DataHandler()方法:进行数据包装

//对邮件的正文部分进行处理      
//MimeMultipart bodyMultipart = new MimeMultipart("multipart/related");
//QQ邮箱使用下面这种方式
MimeMultipart bodyMultipart = new MimeMultipart("related");
content.setContent(bodyMultipart);//注意这儿也是不可缺少
MimeBodyPart htmlBodyPart = new MimeBodyPart();//
MimeBodyPart gtfBodyPart = new MimeBodyPart();//图片
gtfBodyPart.setContent("哈哈哈!", "text/html;charset=UTF-8");
htmlBodyPart.setContent("我只是想来测试一下。。。。。。。。。", "text/html;charset=utf-8");

bodyMultipart.addBodyPart(gtfBodyPart);
bodyMultipart.addBodyPart(htmlBodyPart);
DataSource gtfds = new FileDataSource("1.jpg");
DataHandler gtfdh = new DataHandler(gtfds);
gtfBodyPart.setDataHandler(gtfdh);

msg.saveChanges();
//transport用于发送
Transport transport = session.getTransport();
//建立连接,QQ邮箱服务器地址smtp.qq.com,端口号:465,
//你的账户382816843@qq.com,
//注意ntulsxbdknfkbjej为授权码,并不是密码
transport.connect("smtp.qq.com",465, "382816843@qq.com", "ntulsxbdknfkbjej");
//设置接受方邮件的地址
transport.sendMessage(msg, new Address[]{new InternetAddress("3601905481@qq.com")});
        transport.close();//最后记得关闭连接
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java语言,便利邮件Content内容的代码示例: ```java import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Base64; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class EmailContentBuilder { private MimeMessage message; private Multipart multipart; public EmailContentBuilder(MimeMessage message) throws MessagingException { this.message = message; this.multipart = new MimeMultipart(); this.message.setContent(this.multipart); } public void setText(String text) throws MessagingException { BodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(text); this.multipart.addBodyPart(bodyPart); } public void setHtml(String html) throws MessagingException { BodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(html, "text/html"); this.multipart.addBodyPart(bodyPart); } public void addAttachment(String filePath) throws IOException, MessagingException { File file = new File(filePath); DataSource dataSource = new FileDataSource(file); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(dataSource)); bodyPart.setFileName(file.getName()); this.multipart.addBodyPart(bodyPart); } public void addInlineImage(String filePath, String contentId) throws IOException, MessagingException { File file = new File(filePath); DataSource dataSource = new FileDataSource(file); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(dataSource)); bodyPart.setHeader("Content-ID", "<" + contentId + ">"); this.multipart.addBodyPart(bodyPart); } public void addBase64EncodedImage(String base64Content, String contentId) throws MessagingException { byte[] bytes = Base64.getDecoder().decode(base64Content); DataSource dataSource = new ByteArrayDataSource(bytes, "image/png"); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(dataSource)); bodyPart.setHeader("Content-ID", "<" + contentId + ">"); this.multipart.addBodyPart(bodyPart); } private static class ByteArrayDataSource implements DataSource { private byte[] bytes; private String contentType; public ByteArrayDataSource(byte[] bytes, String contentType) { this.bytes = bytes; this.contentType = contentType; } @Override public InputStream getInputStream() { return new ByteArrayInputStream(this.bytes); } @Override public OutputStream getOutputStream() { throw new UnsupportedOperationException("Not supported"); } @Override public String getContentType() { return this.contentType; } @Override public String getName() { return null; } } } ``` 上述代码实现了一个邮件内容构造器,支持添加文本、HTML、附件、内联图片和Base64编码图片。使用示例: ```java MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("My Email Subject"); EmailContentBuilder contentBuilder = new EmailContentBuilder(message); contentBuilder.setText("Hello world!"); contentBuilder.setHtml("<h1>Hello world!</h1>"); contentBuilder.addAttachment("/path/to/myfile.pdf"); contentBuilder.addInlineImage("/path/to/myimage.png", "myimage"); contentBuilder.addBase64EncodedImage("iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AA...", "mybase64image"); Transport.send(message); ``` 上述代码会发送一封邮件,包含文本、HTML、附件、内联图片和Base64编码图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值