ASP.NET Core 如何在 JavaScript 中将 IEnumerable 字符串作为变量传递
ASP.NET Core How to pass an IEnumerable String as a Variable in JavaScript
我想使用 MimeKit 将收件人邮箱地址作为参数传递给多个地址以发送消息。
如果我编辑控制器操作以包含一个包含 2 个电子邮件地址的字符串:
var message = new Message(new string[] { "address1@gmail.com","address2@gmail.com" }, eMailSubject, eMailContent, null);
邮件将发送给两个收件人 address1@gmail.com 和 address2@gmail.com。
但是,如果我尝试从函数 email_valueChanged(e) 传递参数字符串 eMailTo,我会收到错误消息:
$exception {“Invalid addr-spec token at offset 0”}
x "\"address1@gmail.com\",\"address2@gmail.com\""
有没有办法在 javascript 中为 IActionResult SendMessage 传递 eMailTo 参数?请注意,to 是一个 IEnumerable 字符串:
public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection attachments)
我也曾尝试将 eMailTo 参数作为数组传递,但没有成功。
详情如下:
从弹出窗口发送电子邮件的函数:
function showInfo(data) { location.href = '@Url.Action("SendMessage", "EMail")?eMailTo=' + eMailToAddress +'&eMailSubject=' + eMailSubject + '&eMailContent=' + eMailContent;}
<script>
let eMailToAddress = -1;
let eMailSubject = -1;
let eMailContent = -1;
function email_valueChanged(e) {
eMailToAddress = '"address1@gmail.com","address2@gmail.com"';
console.log("eMailToAddress = " + eMailToAddress);
}
</script>
EMailController.cs
using EmailService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CPSPMO.Controllers
{
public class EmailController : Controller
{
private readonly IEmailSender _emailSender;
public EmailController(IEmailSender emailSender)
{
_emailSender = emailSender;
}
public IActionResult SendMessage(string eMailTo, string eMailSubject, string eMailContent)
{
var message = new Message(new string[] { eMailTo }, eMailSubject, eMailContent, null);
_emailSender.SendEmailAsync(message);
return NoContent();
}
}
}
EMailConfiguration.cs
namespace EmailService
{
public class EmailConfiguration
{
public string From { get; set; }
public string SmtpServer { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
EMailSender.cs
using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace EmailService
{
public class EmailSender : IEmailSender
{
private readonly EmailConfiguration _emailConfig;
public EmailSender(EmailConfiguration emailConfig)
{
_emailConfig = emailConfig;
}
public void SendEmail(Message message)
{
var emailMessage = CreateEmailMessage(message);
Send(emailMessage);
}
public async Task SendEmailAsync(Message message)
{
var mailMessage = CreateEmailMessage(message);
await SendAsync(mailMessage);
}
private MimeMessage CreateEmailMessage(Message message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;
var bodyBuilder = new BodyBuilder { HtmlBody = String.Format("<h2 style='color:red'>{0}<h2>",message.Content) };
if(message.Attachments != null && message.Attachments.Any())
{
byte[] fileBytes;
foreach (var attachment in message.Attachments)
{
using (var ms = new MemoryStream())
{
attachment.CopyTo(ms);
fileBytes = ms.ToArray();
}
bodyBuilder.Attachments.Add(attachment.FileName, fileBytes, ContentType.Parse(attachment.ContentType));
}
}
emailMessage.Body = bodyBuilder.ToMessageBody();
return emailMessage;
}
private async Task SendAsync(MimeMessage mailMessage)
{
using (var client = new SmtpClient())
{
try
{
await client.ConnectAsync(_emailConfig.SmtpServer, _emailConfig.Port, true);
await client.AuthenticateAsync(_emailConfig.Username, _emailConfig.Password);
await client.SendAsync(mailMessage);
}
catch
{
throw;
}
finally
{
await client.DisconnectAsync(true);
client.Dispose();
}
}
}
}
}
IMailSender.cs
using System.Threading.Tasks;
namespace EmailService
{
public interface IEmailSender
{
void SendEmail(Message message);
Task SendEmailAsync(Message message);
}
}
Message.cs
using Microsoft.AspNetCore.Http;
using MimeKit;
using System.Collections.Generic;
using System.Linq;
namespace EmailService
{
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public IFormFileCollection Attachments { get; set; }
public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection attachments)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x)));
Subject = subject;
Content = content;
Attachments = attachments;
}
}
}
请尝试使用string[] eMailTo
,它应该对你有用。
public string TestArray(string[] eMailTo,string eMailSubject)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < eMailTo.Length; i++)
{
msg.Append(eMailTo[i]);
msg.Append(",");
}
return msg.Append(eMailSubject).ToString();
}
测试Link:
https://localhost:44374/home/TestArray?eMailTo=address1@gmail.com&eMailTo=address2@gmail.com&eMailTo=address3@gmail.com&eMailSubject=testdata
测试结果:
我想使用 MimeKit 将收件人邮箱地址作为参数传递给多个地址以发送消息。
如果我编辑控制器操作以包含一个包含 2 个电子邮件地址的字符串:
var message = new Message(new string[] { "address1@gmail.com","address2@gmail.com" }, eMailSubject, eMailContent, null);
邮件将发送给两个收件人 address1@gmail.com 和 address2@gmail.com。
但是,如果我尝试从函数 email_valueChanged(e) 传递参数字符串 eMailTo,我会收到错误消息:
$exception {“Invalid addr-spec token at offset 0”}
x "\"address1@gmail.com\",\"address2@gmail.com\""
有没有办法在 javascript 中为 IActionResult SendMessage 传递 eMailTo 参数?请注意,to 是一个 IEnumerable 字符串:
public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection attachments)
我也曾尝试将 eMailTo 参数作为数组传递,但没有成功。
详情如下:
从弹出窗口发送电子邮件的函数:
function showInfo(data) { location.href = '@Url.Action("SendMessage", "EMail")?eMailTo=' + eMailToAddress +'&eMailSubject=' + eMailSubject + '&eMailContent=' + eMailContent;}
<script>
let eMailToAddress = -1;
let eMailSubject = -1;
let eMailContent = -1;
function email_valueChanged(e) {
eMailToAddress = '"address1@gmail.com","address2@gmail.com"';
console.log("eMailToAddress = " + eMailToAddress);
}
</script>
EMailController.cs
using EmailService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CPSPMO.Controllers
{
public class EmailController : Controller
{
private readonly IEmailSender _emailSender;
public EmailController(IEmailSender emailSender)
{
_emailSender = emailSender;
}
public IActionResult SendMessage(string eMailTo, string eMailSubject, string eMailContent)
{
var message = new Message(new string[] { eMailTo }, eMailSubject, eMailContent, null);
_emailSender.SendEmailAsync(message);
return NoContent();
}
}
}
EMailConfiguration.cs
namespace EmailService
{
public class EmailConfiguration
{
public string From { get; set; }
public string SmtpServer { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
EMailSender.cs
using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace EmailService
{
public class EmailSender : IEmailSender
{
private readonly EmailConfiguration _emailConfig;
public EmailSender(EmailConfiguration emailConfig)
{
_emailConfig = emailConfig;
}
public void SendEmail(Message message)
{
var emailMessage = CreateEmailMessage(message);
Send(emailMessage);
}
public async Task SendEmailAsync(Message message)
{
var mailMessage = CreateEmailMessage(message);
await SendAsync(mailMessage);
}
private MimeMessage CreateEmailMessage(Message message)
{
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress(_emailConfig.From));
emailMessage.To.AddRange(message.To);
emailMessage.Subject = message.Subject;
var bodyBuilder = new BodyBuilder { HtmlBody = String.Format("<h2 style='color:red'>{0}<h2>",message.Content) };
if(message.Attachments != null && message.Attachments.Any())
{
byte[] fileBytes;
foreach (var attachment in message.Attachments)
{
using (var ms = new MemoryStream())
{
attachment.CopyTo(ms);
fileBytes = ms.ToArray();
}
bodyBuilder.Attachments.Add(attachment.FileName, fileBytes, ContentType.Parse(attachment.ContentType));
}
}
emailMessage.Body = bodyBuilder.ToMessageBody();
return emailMessage;
}
private async Task SendAsync(MimeMessage mailMessage)
{
using (var client = new SmtpClient())
{
try
{
await client.ConnectAsync(_emailConfig.SmtpServer, _emailConfig.Port, true);
await client.AuthenticateAsync(_emailConfig.Username, _emailConfig.Password);
await client.SendAsync(mailMessage);
}
catch
{
throw;
}
finally
{
await client.DisconnectAsync(true);
client.Dispose();
}
}
}
}
}
IMailSender.cs
using System.Threading.Tasks;
namespace EmailService
{
public interface IEmailSender
{
void SendEmail(Message message);
Task SendEmailAsync(Message message);
}
}
Message.cs
using Microsoft.AspNetCore.Http;
using MimeKit;
using System.Collections.Generic;
using System.Linq;
namespace EmailService
{
public class Message
{
public List<MailboxAddress> To { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public IFormFileCollection Attachments { get; set; }
public Message(IEnumerable<string> to, string subject, string content, IFormFileCollection attachments)
{
To = new List<MailboxAddress>();
To.AddRange(to.Select(x => new MailboxAddress(x)));
Subject = subject;
Content = content;
Attachments = attachments;
}
}
}
请尝试使用string[] eMailTo
,它应该对你有用。
public string TestArray(string[] eMailTo,string eMailSubject)
{
StringBuilder msg = new StringBuilder();
for (int i = 0; i < eMailTo.Length; i++)
{
msg.Append(eMailTo[i]);
msg.Append(",");
}
return msg.Append(eMailSubject).ToString();
}
测试Link:
https://localhost:44374/home/TestArray?eMailTo=address1@gmail.com&eMailTo=address2@gmail.com&eMailTo=address3@gmail.com&eMailSubject=testdata
测试结果: