如何在发送邮件之前验证vb.net中的smtp凭据 [英] how to validate smtp credentials in vb.net before sending a mail

查看:52
本文介绍了如何在发送邮件之前验证vb.net中的smtp凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

This is my code:

using System.Net;
using System.Net.Mail;

// Before this code i need validate smtp credentials

SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential = 
    new NetworkCredential("username", "password"); 
MailMessage message = new MailMessage(); 
MailAddress fromAddress = new MailAddress("from@yourdomain.com"); 

smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;

message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("to@anydomain.com"); 

try
{
    smtpClient.Send(message);
}
catch(Exception ex)
{
    //Error, could not send the message
    Response.Write(ex.Message);
}

推荐答案

SO:我可以在调用client.Send()之前测试SmtpClient吗? [ ^ ]可以给你一些线索。
SO: Can I test SmtpClient before calling client.Send()?[^] could give you some clues.


这篇关于如何在发送邮件之前验证vb.net中的smtp凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆