【问题标题】:how to send attachment using sendgrid mail api如何使用 sendgrid 邮件 api 发送附件
【发布时间】:2012-10-11 13:42:44
【问题描述】:

我需要使用 sendgrid api 在 java 中发送带有附件的邮件。我提到像[filename.xls]=bytearray 这样的文件。我正在使用 jxl api 获取这个字节数组。我可以发送邮件,但xls 没有这些值。

代码:

sendgridUrl+"?api_user="+apiUser + "&api_key=" + apiPassword + ""
            + to + "&subject=" + (subject.replaceAll(" ", "%20")) + "&fromname="
            + senderPersonal + "&files[" + fileName + "]=" + value2
            + "&html=" + mess‌​age + "&from=" + AppConfig.getProperty("senderemail");

【问题讨论】:

  • 分享你正在使用的代码对调试非常有帮助。
  • sendgridUrl+"?api_user="+apiUser+"&api_key="+apiPassword+"" + to+"&subject="+(subject.replaceAll(" ", "%20"))+"&fromname=" +senderPersonal+"&files["+fileName+"]="+value2+"&html="+message+"&from="+AppConfig .getProperty("senderemail");
  • 这没有多大帮助,因为我不知道任何变量是什么。请使用有问题的整个代码 sn-p 更新实际问题。

标签: java email attachment sendgrid


【解决方案1】:
private static bool SendMailUsingSendgrid(EmailSendRequestDTO request)
        {
            try
            {
                string htmlContent = string.Empty;
                string plainTextContent = string.Empty;

                // Set apikey
                var apiKey = ConfigReader.Read(CommonEnums.ConfigKeys.SendgridApiKey);

                // Initialize sendgrid client.
                var client = new SendGridClient(apiKey);

                // From address
                var from = new EmailAddress(request.FromAddress, request.FromDisplayName);

                // Subject
                var subject = request.Subject;

                // Send mail to multiple recepients.
                var recepientList = new List<EmailAddress>();

                if (!string.IsNullOrEmpty(request.ToAddresses))
                {
                    foreach (var toAddress in request.ToAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        recepientList.Add(new EmailAddress(toAddress));
                    }
                }

                // If content in html format
                if (request.IsHtmlFormat)
                {
                    htmlContent = request.Body;
                }
                else
                {
                    plainTextContent = request.Body;
                }

                // Creat object to send mail message.
                var mailMessage = MailHelper.CreateSingleEmailToMultipleRecipients(from, recepientList, subject, plainTextContent, htmlContent);

                // add multiple cc.
                var ccList = new List<EmailAddress>();

                if (!string.IsNullOrEmpty(request.CCAddresses))
                {
                    foreach (var ccAddress in request.CCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        ccList.Add(new EmailAddress(ccAddress));
                    }
                }

                // add multiple bcc.
                var bccList = new List<EmailAddress>();

                if (!string.IsNullOrEmpty(request.BCCAddresses))
                {
                    foreach (var bccAddress in request.BCCAddresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        bccList.Add(new EmailAddress(bccAddress));
                    }
                }

                // Attachments
                if (!string.IsNullOrEmpty(request.AttachmentFile))
                {
                    foreach (var attachmentFile in request.AttachmentFile.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        SendGrid.Helpers.Mail.Attachment messageAttachment = new SendGrid.Helpers.Mail.Attachment();
                        mailMessage.Attachments.Add(messageAttachment);
                    }
                }

                var response = client.SendEmailAsync(mailMessage).Result;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

【讨论】:

    猜你喜欢
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多