I have been sending email from C# and they are working fine, but now I am also looking for the functionality to allow receiver to unsubscribe. I am unable to get how to proceed as I have no idea. I googled enough so need some hint.
Below is my code:
MailMessage loginInfo = new MailMessage();
loginInfo.To.Add(EmailTxt.Text.ToString());
loginInfo.From = new MailAddress(sEmailId);
loginInfo.Subject = "Subject";
loginInfo.Body = "Your username is: " +;
loginInfo.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = sHost;
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Credentials = new System.Net.NetworkCredential("", "");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(loginInfo);
Unsubscribing is easy if you are already maintaining the users email id's in Database.
Please follow the below steps:
Step 1: Create an extra column in user table as unsubscribe
in database.
it will take true
or false
as values.
set default to false
so that every subscribed user gets email.
Note : Before sending Mails to users please check their unsubscribe
column.
if it is false send an Email.
if it is true don't send an Email as they have unsubscribed.
Step2: Create an unsubscribing URL as below:
http://mywebsite.com/unsubscribeme/emailID=xyz@gmail.com
Step3: Send this URL to user as unsubscribe URL so that whenever he feels to unsubscribe he can do that simply by clicking on that URL.
Step4: once if user clicks on given URL read the QueryString value of emailID emailID=xyz@gmail.com
Step5: Update the user table info by setting the unsubscribe
column value to true
.
Example :
//get user EmailID by QueryString as below:
String EmailID=Reques.QueryString["emailID"].ToString();
//Update the usertable as below:
String Command ="update usertable set unsubscribe='true' where emailid='"+EmailID+"'";