Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. May I know how to send an email as text using visual studio 2010?
It is related to SMTP something.
Can I know where can i get related tutorials or steps to learn? I am new to codings.

Thank You
Posted

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            //string path = "D:\\Test.txt";
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add("imran3may@gmail.com");
                mail.To.Add("shi01715@yahoo.com");
                mail.From = new MailAddress("shishir64092@gmail.com");
                mail.Subject = "Send Email by asp.net code using google or gmail smtp server";
 
                string Body = "Hi, I am testing Email function in asp.net" +
                              " using google or gmail smtp server"+
                              " and next time I will send you a document from my .aspx file";
                mail.Body = Body;
                Attachment at = new Attachment("D:\\Text.txt");
                mail.Attachments.Add(at);
 
                mail.IsBodyHtml = true;
                
                SmtpClient smtp = new SmtpClient("localhost",25);
                smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address  
                smtp.Credentials = new System.Net.NetworkCredential
                     ("shishir64092@gmail.com", "abcdef");
                //Or your Smtp Email ID and Password  
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;
                
                smtp.Send(mail);
                Label1.Text = "Mail Send...";
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
        }

in mail.To.Add you set the mail target address, and in Credentials you set the mail sender address with password!
 
Share this answer
 
Comments
Maciej Los 7-Jun-12 16:04pm    
+5!
C#
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("luckyperson@online.microsoft.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From@online.microsoft.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);
 
Share this answer
 
v2
Comments
Maciej Los 7-Jun-12 16:04pm    
+5!
VB
Imports System.Net.Mail

Partial Class Newsletter_admin
    Inherits System.Web.UI.Page

    Private Function MessageBox() As Object
        Throw New NotImplementedException
    End Function

    Protected Sub Reset_Click(sender As Object, e As System.EventArgs) Handles Reset.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
    End Sub

    Protected Sub SendMail_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SendMail.Click

        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
Net.NetworkCredential("fionatan06@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
            mail = New MailMessage()
            mail.From = New MailAddress("fionatan06@gmail.com")
            mail.To.Add(TextBox2.Text)
            mail.Subject = TextBox3.Text
            mail.IsBodyHtml = True
            mail.Body = TextBox4.Text + "<br></br><br></br>This is an auto-generated message. Please do not reply. Thank you.<br></br><br></br>Colore"
            Dim MyAttachment As Attachment = New Attachment("C:\Users\Admin\Desktop\EAIPJ(Coloré-Rachel,Peiwen,Sherry,Fiona)\Coloré\Images\BLACK SHATTER.jpg")
            mail.Attachments.Add(MyAttachment)
            SmtpServer.Send(mail)
            MsgBox("Mail Send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
 
Share this answer
 
v3

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