栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中通过Amazon发送带有附件的电子邮件的示例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中通过Amazon发送带有附件的电子邮件的示例

也许有些迟了,但是您可以使用以下代码(您还需要Java Mail):

public class MailSender{      private Transport AWSTransport;      ...      //Initialize transport      private void initAWSTransport() throws MessagingException      {        String keyID = <your key id>        String secretKey = <your secret key>        MailAWSCredentials credentials = new MailAWSCredentials();        credentials.setCredentials(keyID, secretKey);        AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(credentials);        Properties props = new Properties(); props.setProperty("mail.transport.protocol", "aws");        props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());        props.setProperty("mail.aws.password", credentials.getAWSSecretKey());        AWSsession = Session.getInstance(props);        AWStransport = new AWSJavaMailTransport(AWSsession, null);        AWStransport.connect();      }  public void sendEmail(byte[] attachment)  {    //mail properties    String senderAddress = <Sender address>;    String recipientAddress = <Recipient address>;    String subject = <Mail subject>;    String text = <Your text>;    String mimeTypeOfText = <MIME type of text part>;    String fileMimeType = <MIME type of your attachment>;    String fileName = <Name of attached file>;    initAWSTransport();    try    {      // Create new message      Message msg = new MimeMessage(AWSsession);      msg.setFrom(new InternetAddress(senderAddress));      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));      msg.setSubject(subject);      //Text part      Multipart multipart = new MimeMultipart();      BodyPart messageBodyPart = new MimeBodyPart();      messageBodyPart.setContent(text, mimeTypeOfText);      multipart.addBodyPart(messageBodyPart);      //Attachment part      if (attachment != null && attachment.length != 0)      {        messageBodyPart = new MimeBodyPart();        DataSource source = new ByteArrayDataSource(attachment,fileMimeType);        messageBodyPart.setDataHandler(new DataHandler(source));        messageBodyPart.setFileName(fileName);        multipart.addBodyPart(messageBodyPart);      }      msg.setContent(multipart);      //send message      msg.saveChanges();      AWSTransport.sendMessage(msg, null);    } catch (MessagingException e){...}  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/440498.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号