使用Stream作为邮件附件

这篇博客介绍了如何利用List获取附件,并在MailMessage中使用这些附件来发送邮件。
摘要由CSDN通过智能技术生成
Attachment类的语法定义如下:
public class Attachment : AttachmentBase
该类的构造函数有6个重载,下面演示创建一个Attachment类的实例的两种常用方式。
Attachment Item = new Attachment (@“c:\附件.txt”, MediaTypeNames.Text.Plain);
以上这种情况,第一个参数为附件的路径,第二个参数为附件的MIME内容标头信息,简单来说就是标明文件的格式。
System.IO.FileInfo file =new System.IO.FileInfo(@"C:\附件.txt");
System.IO.FileStream stream = file.OpenRead();
Attachment item = new Attachment(stream, MediaTypeNames.Text.Plain);
第二种情况,第一个参数以数据流的方式传入。数据流可以从文件中读出,也可以从数据库中读出。第二个参数跟上一种情况相同。

下面的例子已 MemoryStream的方式作为附件,也可以通过 FileStream的方式。

 protected void btnSend_Click(object sender, EventArgs e)
        {
           //获取附件列表

    List<MyAttachment> li = new MyAttachment().GetItems(); 

            MailMessage messa

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要从远程文件服务器拿取附件并将其作为邮件附件发送,可以使用Java的URL类和URLConnection类来实现。以下是一个示例代码,可以实现从远程文件服务器获取附件并将其作为附件添加到邮件中并发送邮件: ```java import java.io.*; import java.net.*; import java.util.*; import javax.activation.*; import javax.mail.*; import javax.mail.internet.*; public class SendEmailWithRemoteAttachment { public static void main(String[] args) { // 远程文件信息 String remoteFileURL = "http://example.com/file.txt"; String remoteFileName = "file.txt"; // 邮件信息 String host = "smtp.example.com"; String from = "your-email@example.com"; String password = "your-email-password"; String to = "recipient@example.com"; String subject = "Email with Remote Attachment"; String body = "Please see the attached file."; // 设置邮件属性 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); // 创建Session Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 获取远程文件 URL url = new URL(remoteFileURL); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); // 创建邮件对象 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); // 创建邮件内容 Multipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body); multipart.addBodyPart(messageBodyPart); // 创建附件 messageBodyPart = new MimeBodyPart(); DataSource source = new ByteArrayDataSource(is, "application/octet-stream"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(remoteFileName); multipart.addBodyPart(messageBodyPart); // 设置邮件内容 message.setContent(multipart); // 发送邮件 Transport.send(message); System.out.println("Email with Remote Attachment sent successfully!"); } catch (MessagingException | IOException e) { throw new RuntimeException(e); } } } ``` 需要注意的是,这个示例代码中的远程文件地址、远程文件名、邮件服务器地址、发件人邮箱、发件人邮箱密码、收件人邮箱需要根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值