Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends..
I am creating a page in asp.net to send mails, where Credentials ('userid'and 'password') and From are should be different.

for example:
if I am sending mail using creadentials,
UserID: abc@gmail.com
password:123456

then

mail received by recipient should appear as

From : xyz@gmail.com
To : recipient@gmail.com
Subjet: Hi
Message: Hello Dear..

Please notice the 'From : '

Please help..
Here is the code that I am using...


System.Net.NetworkCredential _Credential = new System.Net.NetworkCredential(UserID, Password);
            System.Net.Mail.MailMessage _MailMessage = new MailMessage();
            _MailMessage.To.Add(txt_email.Text);
            _MailMessage.Subject = subject;

            _MailMessage.From = new System.Net.Mail.MailAddress(<big>From</big>);
            _MailMessage.Body = message;
            _MailMessage.IsBodyHtml = true;
            System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient(smtp);
            _SmtpClient.UseDefaultCredentials = true;
            _SmtpClient.EnableSsl = true;
            _SmtpClient.Credentials = _Credential;
            _SmtpClient.Port = Convert.ToInt32(port);
            _SmtpClient.Send(_MailMessage);

            lbl_Err.Text = "Mail Sent Successfully!";
            lbl_Err.ForeColor = System.Drawing.Color.Green;
            lbl_Err.Visible = true;
Posted

When you set the From address, there is an override which allows this:
C#
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);

If you have a look here there is a Tip which provides a generic method for the whole process: Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
Comments
djrocks0101 2-Apr-12 6:54am    
Thanks OriginalGriff, One more thing I wanna know that, If I wish to send mail body formated as web page then how should I do that..please help..
member60 2-Apr-12 7:09am    
check my solution for this that might help you.
OriginalGriff 2-Apr-12 7:33am    
The tip already does that...
member60 2-Apr-12 7:44am    
well ,i had seen only comment not tip, somehow these two lines are more and descriptive i think
djrocks0101 2-Apr-12 7:15am    
thanks..
By default, email sent with System.Web.Mail is formatted as plain text. To format as Html, set the MailMessage.BodyFormat property to MailFormat.Html.

ex:
XML
mail.BodyFormat = MailFormat.Html;
mail.Body = "this is my test email body.<br><b>this part is in bold</b>";
 
Share this answer
 
v2
Comments
djrocks0101 2-Apr-12 7:14am    
thanks member60..my 5..:)
try this
MailAddress mailfrom = new MailAddress ( "frommail@gmail.com" );
            MailAddress mailto = new MailAddress ( "tomail@gmail.com" );
            MailMessage newmsg = new MailMessage ( mailfrom, mailto );
            newmsg.Subject = "Subject of Email";
            newmsg.Body = "Body(message) of email";
            ////For File Attachment, more file can also be attached
            Attachment att = new Attachment ( "G:\\code.txt" );
            newmsg.Attachments.Add ( att );
            SmtpClient smtps = new SmtpClient ( "smtp.gmail.com", 587 );
            smtps.UseDefaultCredentials = false;
            smtps.Credentials = new NetworkCredential ( "urmail@gmail.com", "urpwd" );
            smtps.EnableSsl = true;
            smtps.Send ( newmsg );
 
Share this answer
 

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