这是我的代码:
if (Recipients_To != null) {
for (int i = 0; i < Recipients_To.length; i++) {
message.setRecipients(Message.RecipientType.TO, Recipients_To[i].toString());
Transport.send(message);
}
}
我有超过500个收件人列表发送邮件,此代码将向每个收件人发送个人邮件。但是,如果我在这个for循环之间遇到异常,我想继续循环以保留剩余的收件人。我该怎么办?
答案 0 :(得分:4)
您可以捕获例外情况。
try {
message.setRecipients(Message.RecipientType.TO, Recipients_To[i].toString());
Transport.send(message);
} catch (Exception e) {
// handle it or leave it be
}
答案 1 :(得分:3)
从技术上讲,这只是捕捉异常的问题(参见Murat K的回答)。但是,我建议,既然您正在发送电子邮件,那么在第一次发生异常时您会停止发送其余内容,除非您确定这是一个错误,您可以放心地忽略。一些可能出错的事例:
因此,请在处理电子邮件时仔细考虑您的行动。
答案 2 :(得分:2)
您希望使用catch
for (int i = 0; i < Recipients_To.length; i++)
{
try {
message.setRecipients(Message.RecipientType.TO,Recipients_To[i].toString());
Transport.send(message);
}
catch (YourException e){
//Do some thing to handle the exception
}
}
块来执行此操作,如此
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ClientId = "xxx-xxxxx-xxxx";
options.Authority = "xxx-xxxxx-xxxx";
options.CallbackPath = "/signin-oidc";
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = ctx =>
{
ctx.ProtocolMessage.RedirectUri = $"https://reverse_proxy_url/app_name/service_name{options.CallbackPath}";
return Task.CompletedTask;
}
};
});
这可以捕获潜在的问题,并且一旦处理了异常,它仍将继续使用for循环。
答案 3 :(得分:0)
您可以尝试这样的事情
if (Recipients_To != null) {
for (int i = 0; i < Recipients_To.length; i++) {
try {
message.setRecipients(Message.RecipientType.TO, Recipients_To[i].toString());
Transport.send(message);
} catch (Exception exp) {
//Log your exception here or do whatever you want to happen in case of exception
}
}
}