“String”类型的值无法转换为“System.Net.Mail.MailMessage” [英] Value of type 'String' cannot be converted to 'System.Net.Mail.MailMessage'
问题描述
您好b $ b
我在使用Asp.net发送电子邮件时遇到问题。错误是'String'类型的值无法转换为'System.Net.Mail.MailMessage'
我不知道我在哪里做错了。
请给我建议
Maideen
以下是代码
web.config
Hi
I have an issue in sending email using Asp.net. error is Value of type 'String' cannot be converted to 'System.Net.Mail.MailMessage'
I do not know where i did wrong.
Pls advice me
Maideen
Below is code
web.config
<system.net>
<mailSettings>
<smtp from="xxxxxxx@gmail.com">
<network host="smtp.gmail.com"
password="xxxxxx"
userName="xxxxxx@gmail.com"
enableSsl="true"
port="587"/>
</smtp>
</mailSettings>
</system.net>
功能
Function
Private Function PopulateBody() As String
Dim body As String = String.Empty
Dim reader As StreamReader = New StreamReader(Server.MapPath("comlaint_log.html"))
body = reader.ReadToEnd
body = body.Replace("{Date}", Date.Now)
body = body.Replace("{DocNo}", Me.txtDocNo.Text)
body = body.Replace("{name}", Me.txtName.Text)
body = body.Replace("{NComplaints}", Me.cboNComplaints.SelectedValue)
body = body.Replace("{IssueNo}", Me.txtIssueNo.Text)
body = body.Replace("{Remark}", Me.txtComplaints.Text)
body = body.Replace("{Code}", Me.txtCode.Text)
body = body.Replace("{SAgentCode}", Me.txtSAgentCode.Text)
body = body.Replace("{BAddress1}", Me.txtBAddress1.Text)
body = body.Replace("{BAddress2}", Me.txtBAddress2.Text)
body = body.Replace("{poscode}", Me.txtPosCode.Text)
body = body.Replace("{city}", Me.txtCity.Text)
body = body.Replace("{state}", Me.txtState.Text)
body = body.Replace("{country}", Me.txtCountry.Text)
body = body.Replace("{Status}", Me.cboStatus.SelectedValue)
Return body
End Function
Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)
Dim client As New SmtpClient()
client.Send(body)
End Sub
Protected Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
Dim body As String = Me.PopulateBody
Me.SendHtmlFormattedEmail(Me.txtEmail.Text, ".", body)
Session.Remove("email")
End Sub
请给我建议
Maideen
Pls advice me
Maideen
推荐答案
你正在使用错误的变种SmtpClient.Send
线索:它在哪里,主题是什么?
you're using the wrong variant ofSmtpClient.Send
clue: where's it going, what's the subject?
这是因为你只将一个字符串传递给 SmptpClient.Send()方法。
此方法有两种形式,即它有一个重载。第一个表单采用 MailMessgae <类型的1个参数/ a>这就是你在代码中使用的内容。你需要有以下信息
1.来自(电子邮件ID),
2.到(电子邮件ID),
3.主题和放大器;
4.正文
但是在你的代码中你只有 body 。
而不是 -
This is because you passing just a string to the SmptpClient.Send() method.
This method has two forms i.e, it has one overload. The first form takes 1 parameter of type MailMessgae and this is what you have used in your code. You need to have following information
1. from (email id),
2. to (email id),
3. subject &
4. body
But in your code you have only body .
Instead of -
client.Send(body)
试试这个 -
Try this-
client.Send(fromaddress@yourdomain.com,toaddress@yourdomain.com,"Your Subject",body)
希望,它有帮助:)
阅读这些文件
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.send%28v=vs.110%29.aspx [ ^ ]
https://msdn.microsoft.com/en-us/library/h1s04he7%28v=vs.110%29.aspx [ ^ ]
https://msdn.microsoft.com/en- us / library / swas0fwc%28v = vs.110%29.aspx [ ^ ]
Hope, it helps :)
Read these documentations
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.send%28v=vs.110%29.aspx[^]
https://msdn.microsoft.com/en-us/library/h1s04he7%28v=vs.110%29.aspx[^]
https://msdn.microsoft.com/en-us/library/swas0fwc%28v=vs.110%29.aspx[^]
嗨
谢谢大家。我已经解决了以下是代码
Hi thank all of you. I have solved Below is code
Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)
'After config mailSettings section in web.config, you don't need to initialization client's property
Dim client As New SmtpClient()
Dim message As New MailMessage()
message.Body = body
message.To.Add(recepientEmail)
message.Subject = subject
message.IsBodyHtml = True
client.Send(message)
End Sub
这篇关于“String”类型的值无法转换为“System.Net.Mail.MailMessage”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!