Click here to Skip to main content
15,063,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
<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

VB
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


Pls advice me

Maideen
Posted
Updated 7-Jul-15 19:42pm
v2

you're using the wrong variant of SmtpClient.Send

clue: where's it going, what's the subject?
   
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 -
VB
client.Send(body)

Try this-
VB
client.Send(fromaddress@yourdomain.com,toaddress@yourdomain.com,"Your Subject",body)


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[^]
   
Comments
Maideen Abdul Kader 8-Jul-15 20:28pm
   
Hi Giri
thank you first of all

It sends mail But in html format. Pls advise.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>
<style>
.text_style
{
/*height: 23px;*/
font-size:25px;
font-family:Calibri,Verdana,Arial,'Times New Roman';
}
.position_image
{
text-align:center;
}

.tbl_size
{
width:750px;
border:double;
}

.text_style_body
{
font-size:14px;
font-family:Calibri,Verdana,Arial,'Times New Roman';
}
.text_style_bold
{
font-weight:bold;
}
.html_formating
{
/*border-collapse:collapse;*/
/*padding:1px;
border: 1px solid black;
padding: 5px;*/
width:750px;
text-align: left;
font-family:Calibri,Verdana,Arial,'Times New Roman';

color: black;
/*border:2px;*/
}

.headercolor {

background-color:#B43104;
border:double;
color:#fff
}
.rowcolor {

background-color: #eee;
background-color:#DF7401;
}

</style>



</head>
<body>
<table class="tbl_size position_image">
<tr>
<td colspan="2" class="position_image"><img src="http://www.silsnetwork.com/edge/TEM.logo_Sendblaster.jpg" /></td>
</tr>
<tr>
<td colspan="2" class="position_image">----------------------------------</td>
</tr>
<tr>
<td colspan="2" class="position_image text_style" >THE EDGE FEEDBACK LOG</td>
</tr>
</table>
<br />
<div class="text_style_body">Date:7/9/2015 8:26:50 AM</div>
<br />
<div class="text_style_body">Complains Log No:- 00035</div>
<br />
<div class="text_style_body">Dear Mr/Mrs/Dato/Datin/Dato Seri/Datin Seri maideen</div>
<p class="text_style_body">Greetings From The EDGE.</p>
<p class="text_style_body">Thank you for your feedback.</p>

<table class="text_style_body">
<tr>
<td>Nature of Complain</td>
<td>:</td>
<td>DELIVERY UP</td>
</tr>
<tr>
<td>Issue No</td>
<td>:</td>
<td>123</td>
</tr>
<tr>
<td>Remarks</td>
<td>:</td>
<td>fgsfgs</td>
</tr>
<tr>
<td>Subscriber ID</td>
<td>:</td>
<td>32824</td>
</tr>
<tr>
<td>Delivery Agent ID</td>
<td>:</td>
<td>B-APD*</td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td>address1</td>
</tr>
<tr>
<td></td>
<td></td>
<td>address2</td>
</tr>
<tr>
<td>Delivery Address</td>
<td>:</td>
<td>address1</td>
</tr>
<tr>
<td>Post Code</td>
<td>:</td>
<td>poscode</td>
</tr>
<tr>
<td>City</td>
<td>:</td>
<td>pj</td>
</tr>
<tr>
<td>State</td>
<td>:</td>
<td>MELAKA Country: MALAYSIA</td>
</tr>
<tr>
<td>Complain Status</td>
<td>:</td>
<td>ACTIVE</td>
</tr>
</table>
<br />
<div class="text_style_bol
Maideen Abdul Kader 8-Jul-15 23:49pm
   
Thank you
I have solved the issue. This is the 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
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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900