0

使用 vb.net 向 Outlook 发送邮件。但出现错误,例如“指定的字符串不是电子邮件地址所需的格式”如何解决这个问题 Dim Var_from As String = TextBox1.Text Dim to_var As String = TextBox2.Text

    Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()


    mailMessage.From = New System.Net.Mail.MailAddress("'" + Var_from + "'")
    mailMessage.To.Add("'" + to_var + "'")
    mailMessage.Subject = "xxx"

    Dim content As String = "<html xmlns='http://www.w3.org/1999/xhtml'><head><style type='text/css'>#mytable { padding: 0;   margin: 0;border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;       border-left: 1px solid #C1DAD7;   border-top: 1px solid #C1DAD7;}caption {padding: 0 0 5px 0; width: 700px;font: Bold 11px 'Trebuchet MS', Verdana, Arial, Helvetica, sans-serif;      text-align: right;}th {   font: bold 11px 'Trebuchet MS', Verdana, Arial, Helvetica, sans-serif;     color: #4f6b72;      border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;border-left: 1px solid #C1DAD7;border-top: 1px solid #C1DAD7;   letter-spacing: 2px; text-transform: uppercase; text-align: left;       padding: 6px 6px 6px 12px; background: #CAE8EA; }td {  border-right: 0px solid #C1DAD7;  border-bottom: 0px solid #C1DAD7; border-left: 0px solid #C1DAD7;   background: #fff;    padding: 6px 6px 6px 12px;  color: #4f6b72;}</style></head><body><table border='0' cellpadding='0' id='mytable'  width='100%'><tr><th height='74' style='text-align:center; font-size:16px;' colspan='6'>CAS E-mail Confirmation <div style='float:right;'><img src='cid:HDIImage1' width='143' height='62' align='left' alt='' style='position:absolute;top:0pc;'/></div></th></tr><tr><td colspan='6' class='style5'>&nbsp;Dear Customer,<br/><br/> Thank you for  registering CAS. Click the below link to complete your verification process.. <br/><br/><br/>CAS CODE  :  <br/><br/><br/><br/></td></tr>  <tr>    <th colspan='6' class='style5'>&nbsp;</th>  </tr></table></body></html>"

    Dim plainTextView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(content, Nothing, "text/plain")

    mailMessage.AlternateViews.Add(plainTextView)
    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

    smtpClient.Host = "xxx"
    smtpClient.Send(mailMessage)
4

1 回答 1

1

您需要使用普通的电子邮件地址 - 您添加的单引号可能会导致错误。没有额外的引号。

mailMessage.From = New System.Net.Mail.MailAddress(Var_from)
mailMessage.To.Add(to_var)
于 2013-01-02T06:15:07.937 回答