Question

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); 
Was it helpful?

Solution

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+"'";

OTHER TIPS

I guess you have your subscribers stored somewhere. So the easy way to unsubscribe is to remove the record from the store by its email-address.

It has nothing to do with the SmtpClient class.

The concept of a mailing list includes a list of email addresses, usually saved in a database. Your code adds the receiver from an "EmailTxt", I guess a textbox?

  • As long as you do not save email addresses somewhere, there is no need to unsubscribe
  • If you save email addresses, then create another file that allows people to unsubcribe.

An unsubscribe file would usually function as follows:

  • receive the email address to unsubscribe as a querystring parameter
  • display this email address in a single textbox, along with an "unsubscribe" button
  • if the user clicks this button, then remove the email address from the database.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top