Click here to Skip to main content
15,669,039 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tries the below code but it shows a error while compiling

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0104	'MailMessage' is an ambiguous reference between 'System.Net.Mail.MailMessage' and 'System.Web.Mail.MailMessage'	Active

what to do

What I have tried:

public void email_send(System.Net.Mail.MailAddress from, System.Net.Mail.MailAddress to)
           MailMessage mail = new MailMessage();
           SmtpClient SmtpServer = new SmtpClient("");
           mail.From = new MailAddress("your");
           mail.Subject = "Test Mail - 1";
           mail.Body = "mail with attachment";

           System.Net.Mail.Attachment attachment;
           attachment = new System.Net.Mail.Attachment("c:/textfile.txt");

           SmtpServer.Port = 587;
           SmtpServer.Credentials = new System.Net.NetworkCredential("your", "your password");
           SmtpServer.EnableSsl = true;


Updated 11-Aug-22 2:42am
Richard MacCutchan 11-Aug-22 7:18am    
The error message is clear: which namespace are you supposed to be using?

1 solution

It is recommended to use the newer System.Net.Mail.MailMessage class.
MailMessage Class (System.Net.Mail) | Microsoft Docs[^]
Then you must place a using statement on top of your code.
using System.Net.Mail;

This here is also worth reading:
What's obsolete in .NET Framework - .NET Framework | Microsoft Docs[^]
Share this answer
Richard Deeming 11-Aug-22 9:53am    
If you're going to use System.Net.Mail, why would you want to add a using System.Web.Mail; statement at the top of your file?
TheRealSteveJudge 11-Aug-22 10:29am    
You're right. I will it correct this nonsense at once! Thank you!

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