how to send shopping item on email of user - c#

I want to send shopping cart item list that user purchased from website to registered user. How to send product list that user purchased with user's information in mvc?
I used for loop in mvc controller to display purchase order list and tried to save it in email body but it gives me error. Please suggest me how to save whole data into email body.
Following is the code snippet I have used for mailing purpose.
MailMessage mail = new MailMessage();
mail.To.Add("receiver");
mail.From = new MailAddress("onlinebartan#onlinebartan.com");
mail.Subject = "OnlineBartan:Thanks For Order";
string Body = "s" + orderproduct[1].OrderId +"sdfs";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtpout.asia.secureserver.net";
smtp.Port = 25;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("onlinebartan#onlinebartan.com", "Mukesh#1980");
smtp.EnableSsl = false;
smtp.Send(mail);

You can loop through list and append in string
string Body="";
for(int i=0;i<orderproduct.Count;i++)
Body += "s" + orderproduct[i].OrderId +"sdfs <br />";

You have not specified exact error you are getting but I can immediately see is you should set valid html string to mail.Body property when you're setting mail.IsBodyHtml = true.
Hope this helps.

Related

Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email .

The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

Cant use gmail smtp to send email via c# form app

I wrote this code in c# .net form app to send emails. Code is working with yahoo,hotmail,gmx by replacing the smtp servers name but not working with gmail,
try
{
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
var mail = new MailMessage();
mail.From = new MailAddress(youremail.Text);
mail.To.Add(txtreceiver.Text);
mail.Subject = txtsubject.Text;
mail.IsBodyHtml = true;
mail.Body = txtbody.Text;
SmtpServer.Port = 465;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential(youremail.Text, yourpass.Text);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Sent sucessfully..! \n If Email is not found in inbox check junk ");
}
catch (Exception s)
{
MessageBox.Show("Failled To Send Mail..!");
}
Firstly, you must use port 587 as #user1666620 suggested in the comments.
Then you will also need to allow "less secure" devices to access that GMail account. Click on your account avatar, then "My Account" -> "Sign-in & Security" -> "Connected Apps & Sites". At the bottom of that page, toggle the "Allow less secure apps" option.

How do I send an email with Gmail and SmtpClient when the sending account uses two factor authentication?

SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("sender#gmail.com");
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>Hello, this is a demo ... ..</h1>";
message.To.Add("receiver#gmail.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
ex.ToString();
}
// The thing is that this code works fine for gmails without phone number protection. Exception occurs when using this code with gmails that are protected(verified) via the client phone number.
One of the solution is to use a remote server to access clients mails.
Now my question is there another method to solve this issue ? other than third parties.
If I understand you correctly, you're saying the Google account is using two-factor authentication.
If that's the case, you need to create an Application Password for this. Go to https://security.google.com/settings/security/apppasswords once logged in as the account you want to two-factor auth with.
In the list, under Select App choose "Other" and give it some name. Click Generate, and write this password DOWN cause you will only ever see it ONCE. You will use this in your authentication. It will be 16-characters long and the spaces don't matter, you can include them or omit them. I included them here just because.
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();

how to make disable or unvisible email link when the it is clicked, also, how can i use general smtp server?

I tried some codes but after it is send to mail account, it is always the active how can it become passive link .
string UName = GetNewValidationCode();
// Now lets create an email message
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("bilkentliaslan#windowslive.com");
mail.To.Add(TextBox6.Text);
mail.Subject = "Registration";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = string.Format("Thank you for creating an account with YourDomain.com</ br>"+"Please click the below link to activate your account <br />"+"<a href='http://localhost:2386/ActivateUser.aspx?userName{0}&Id={1}'>Activate {0} </a>", UName, user_name);
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("bilkentliaslan#windowslive.com", "my password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
// Redirecto to What ever page
Response.Redirect("Login.aspx");
Keep a table that stores the waiting IDs, i.e., the IDs that you have sent an email about, but which are not yet activated.
Then in the code of your ActivateUser.aspx (actually in the back-end code that that page then calls), check if the row exists, perform the user activation work and then remove the row from the table. If the user then goes to the very same URL again later, the row won't be there and the user won't be (re-)activated.

send smtp mail including html to gmail account

I've found this small code that sends email to gmail users. I'd like the body of the mail to contain html (for example, decoding a link for it to hold different text than the url it's pointing to).
I am using c# .net 3.5. I've used these classes in my code:
MailMessage
SmtpClient
How can this be done?
Here's a copy of my code:
MailMessage message = new MailMessage("me#gmail.com", WebCommon.UserEmail, "Test", context.Server.HtmlEncode("<html> <body> <a href='www.cnn.com'> test </a> </body> </html> "));
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("him#gmail.com", "myPwd");
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.UseDefaultCredentials = false;
smtp.EnableSsl = true;
smtp.Credentials = cred;
smtp.Port = 587;
smtp.Send(message);
Thanks!
Something like this should work:
Note that MailMessage refers to System.Net.MailMessage. There is also System.Web.MailMessage, which I have never used and -as far as I know- is obsolete.
MailMessage message = new MailMessage();
// Very basic html. HTML should always be valid, otherwise you go to spam
message.Body = "<html><body><p>test</p></body></html>";
// QuotedPrintable encoding is the default, but will often lead to trouble,
// so you should set something meaningful here. Could also be ASCII or some ISO
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
// No Subject usually goes to spam, too
message.Subject = "Some Subject";
// Note that you can add multiple recipients, bcc, cc rec., etc. Using the
// address-only syntax, i.e. w/o a readable name saves you from some issues
message.To.Add("someone#gmail.com");
// SmtpHost, -Port, -User, -Password must be a valid account you can use to
// send messages. Note that it is very often required that the account you
// use also has the specified sender address associated!
// If you configure the Smtp yourself, you can change that of course
SmtpClient client = new SmtpClient(SmtpHost, SmtpPort) {
Credentials = new NetworkCredential(SmtpUser, SmtpPassword),
EnableSsl = enableSsl;
};
try {
// It might be necessary to enforce a specific sender address, see above
if (!string.IsNullOrEmpty(ForceSenderAddress)) {
message.From = new MailAddress(ForceSenderAddress);
}
client.Send(message);
}
catch (Exception ex) {
return false;
}
For more sophisticated templating solutions that render the Body html rather than hard-codin it, there is, for example, the EMailTemplateService in MvcContrib which you can use as a guideline.
One way to do it is to create an alternate html view of the email:
MailMessage message = new MailMessage();
message.Body = //plain-text version of message
//set up message...
//create html view
string htmlBody = "<html>...</html>";
htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
message.AlternateViews.Add(htmlView);
//send message
smtpClient.Send(message);

Categories

Resources