java打印被阻止_使用javamail发送邮件的时候如何阻止附件内容输出到控制台

在使用JavaMail发送带有附件的邮件时,遇到Transport.sendMessage()步骤时,附件内容会默认输出到控制台。要阻止这种行为,可以通过设置Session的debug属性为false来关闭调试信息输出。
摘要由CSDN通过智能技术生成

我在使用JavaMail发送带附件的邮件时候,每次到了Transport.sendMessage()这一步,控制台就会输出附件内容,请问如何设置可以取消输出呢?

public void sendFileAttachedMail(String fromMail, String toMail, String fromMailPwd, String bookId) {

Properties prop = new Properties();

prop.setProperty(MAIL_HOST, MAIL_HOST_VALUE);

prop.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_PROTOCOL_VALUE);

prop.setProperty(MAIL_SMTP_AUTH, MAIL_SMTP_AUTH_VALUE);

Session session = Session.getInstance(prop);

session.setDebug(true);

try {

Transport ts = session.getTransport();

String fromMailPrefix = fromMail.split("@")[0];

ts.connect(MAIL_HOST_VALUE,fromMailPrefix, fromMailPwd);

String subject = "FILE ATTACHED MAIL TEST";

String content = "Mail Content RE";

String fileSavePath = "E://attachMail.eml";

Message message = createFileAttachedMail(session, fromMail, toMail, subject, content, bookId, fileSavePath);

ts.sendMessage(message, message.getAllRecipients());

ts.close();

} catch (Exception e) {

if(logger.isErrorEnabled()){

logger.error("send fileAttachedMail failed!",e);

}

}

}

public MimeMessage createFileAttachedMail(Session session, String fromAdd, String toAdd, String subject, String content, String fileObjectId, String fileSavePath) throws Exception {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(fromAdd));

message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAdd));

message.setSubject(subject);

// 邮件正文

MimeBodyPart text = new MimeBodyPart();

text.setContent(content, MAIL_CONTENT_FORMAT_CHARSET);

// 附件

MimeBodyPart attach = new MimeBodyPart();

DataHandler handler = new DataHandler(new FileDataSource(this.gridFSService.readFiles(fileObjectId)));

attach.setDataHandler(handler);

attach.setFileName(handler.getName());

// 创建容器描述数据关系

MimeMultipart mp = new MimeMultipart();

mp.addBodyPart(text);

mp.addBodyPart(attach);

mp.setSubType("mixed");

message.setContent(mp);

message.saveChanges();

//将创建的email写入到本地存储

//message.writeTo(new FileOutputStream(fileSavePath));

return message;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当需要在邮件正文中嵌入图片时,可以使用HTML格式的邮件,并在HTML中嵌入图片的URL。下面是使用JavaMail发送包含图片的HTML格式邮件的详细步骤: 1. 导入JavaMailjavax.activation库。 2. 创建一个MimeMessage对象。 3. 设置邮件的基本信息,包括发件人、收件人、主题等。 4. 创建一个Multipart对象,用于组合邮件正文和图片。 5. 创建一个HTML格式的邮件正文。 6. 创建一个MimeBodyPart对象,用于包装图片。 7. 将图片附件添加到MimeBodyPart对象中。 8. 将MimeBodyPart对象添加到Multipart对象中。 9. 将Multipart对象设置为邮件内容。 10. 发送邮件。 下面是一份示例代码,可以参考: ```java import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendMailWithImage { public static void main(String[] args) { // 发件人电子邮箱 String from = "from@example.com"; // 收件人电子邮箱 String to = "to@example.com"; // 指定发送邮件的主机为 smtp.qq.com String host = "smtp.qq.com"; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.auth", "true"); // 获取默认session对象 Session session = Session.getDefaultInstance(properties, new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("你的QQ账号", "你的QQ邮箱授权码"); //发件人邮件用户名、密码 } }); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject("包含图片的邮件"); // 创建消息部分 BodyPart messageBodyPart = new MimeBodyPart(); // 消息 String messageText = "<h1>这是一封包含图片的HTML格式邮件!</h1><br/><img src=\"cid:image\">"; messageBodyPart.setContent(messageText, "text/html"); // 创建多重消息 Multipart multipart = new MimeMultipart(); // 设置文本消息部分 multipart.addBodyPart(messageBodyPart); // 附件部分 messageBodyPart = new MimeBodyPart(); String filename = "image.png"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setHeader("Content-ID", "<image>"); multipart.addBodyPart(messageBodyPart); // 发送合成消息 message.setContent(multipart); // 发送消息 Transport.send(message); System.out.println("邮件发送成功!"); }catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 其中,需要替换的部分有: - `from@example.com` 为发件人邮箱地址。 - `to@example.com` 为收件人邮箱地址。 - `smtp.qq.com` 为发件人邮箱SMTP服务器地址,可以根据实际情况进行修改。 - `你的QQ账号` 和 `你的QQ邮箱授权码` 分别为发件人邮箱的账号和授权码,需要替换为实际的内容。 - `image.png` 为要添加的图片文件名,需要替换为实际的图片文件名。 运行程序后,将会发送一封包含图片的HTML格式邮件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值