Problem with app sending email followed by two empty emails - c#

My app is sending 3 emails at the same time to the recipient, one being the correct email, and the other two contain the subject line, but an empty message. Could this code some how cause that? If not what do you suggest?
var fromAddress = new MailAddress(domainAddress, displayName);
var toAddress = new MailAddress(oInfo.SiteUser.email, oInfo.customerName);
var Bcc = new MailAddress("deleted");
var smtp = new SmtpClient
{
Host = SmtpHost(),
Port = SmtpPort(),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(SmtpUsername(), SmtpPassword())
};
using (var msg = new MailMessage(fromAddress, toAddress)
{
IsBodyHtml = true,
Subject = "Confirmation for your recent order at " + displayName,
Body = body
})
{
msg.Bcc.Add(Bcc);
smtp.Send(msg);
}

No, that code won't send more than one mail.
Either you have some more code that is sending mail, or you are executing that code three times, but with different values for body.

The only problem I can see with that code is the line
var Bcc = new MailAddress("deleted");
but I assume you modified it for posting here?
I can't see an issue that would cause what you're seeing. I'd check the headers in the emails for clues. Also capturing network traffic on the machine sending the emails could help.

Related

How to send email using ASP.NET, C# and GMail

I have attempted to send an email using the GMail SMTP, and followed the guides on various other questions but I still cannot get emails to send from my GMail account.
This is the code I'm using:
protected void emailSend_Click(object sender, EventArgs e)
{
var fromAddress = new MailAddress(inputEmail.Text, inputName.Text);
var toAddress = new MailAddress("spikey666#live.co.uk", "Liane Stevenson");
const string fromPassword = "*********";
const string subject = "Web Dev Wolf Message";
var body = inputMessage.Text;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("webdevelopwolf#gmail.com", fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
These are the things I've checked so far:
Turning on less secure apps on GMail
Checked the Gmail Username and Password are correct
Debugged and checked that all text fields have values and are loaded into variables
Check other port numbers suggested by Gmail help
Turned on POP/IMAP functionality on Gmail
Is there anything else I could be missing?
Before calling SmtpClient.Send(), add:
smtp.UseDefaultCredentials = false;
According to the MSDN SmtpClient page, UseDefaultCredentials is set to false by default, but there seems to be a bug somewhere that is setting it to true. Explicitly set it to false before sending the message and it should be all set.

sending email failed in .net when using google smtp server

I am trying to send email like this
var fromAddress = new MailAddress("fromaddress", "From Name");
var toAddress = new MailAddress("toaddress", "To Name");
const string fromPassword = "password";
const string subject = "Subject";
const string body = "Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
Console.WriteLine("Sent");
Console.ReadLine();
but it gives this error .
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required.
I am sing this code in simple console application on my local host . So whats the issue in my code ?
Update
I changed fromAddress email and it send email successfully . But i don't receive any email in my toAddress email's inbox/Spam .
Try to add DeliveryMethod = SmtpDeliveryMethod.Network when creating SmtpClient.
See post:
https://stackoverflow.com/a/489594/1432770
There is a variety of reasons for this discussed here:
Sending email through Gmail SMTP server with C#
Your code in the first link has worked for me.
Do you use two steps verification?
You need to sign in using application-specific passwords: https://support.google.com/accounts/answer/185833?hl=en
Your code worked for me too!

Sending emails that requires a secure connection

REMAKING QUESTION
This code doesn't work
The error is: The remote certificate is invalid according to the validation procedure.
var client = new SmtpClient("smtp.domain.com.br", 25000)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom#domain.com.br", "emailto#gmail.com", "test", "testbody");
// I also tested like this
// client.Send("emailfrom#domain.com.br", "emailto#domain.com", "test", "testbody");
But this code works
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("username", "password"),
EnableSsl = true
};
client.Send("emailfrom#domain.com.br", "emailto#gmail.com", "test", "testbody");
I tested the first code data in the Outlook, and works if I try to send to an email from the same domain.
I believe that the error is some SMTP configuration, but I don't know how to solve this. Any help?
which smtp server you using ?
if you are using gmail smtp server then you must use port number 587 that is required to send through gmail
based on the answer below you can pass in the values from the .config file but you will have to change what I have ..test this using hard coded values first if you like then convert the working version to use param values
using System.Net;
using System.Net.Mail;
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
var _smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials =
new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
_smtpClient.Send(message);
}
did you try port 465. may be you can check this article.
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
I solve this using the digital certificate generated in the outlook and putting it on the server, where my application is running.
With the code in my question plus the certificate I finally made this works.
Here is a link on How to do this: create your own digital certificate

How to send automated emails?

I am working in C# and Sql server. I have a table Email that contains fields from, to, subject, body, CC, BCC etc. When something happens in it saves the notification. I want to send the mail notification that newly inserted in Email table automatically. Is there is a way in SQL Server??? Which send emails automatically and deletes the record from the table. OR what is the efficient way for this task (from C#code or else).
Thanks,
Girish
What I would do is set up your sql server to have sql Mail and then use a trigger against your table to fire the mail event and then delete the row.
Here's how to configure your sever for sql mail
SQL Mail Config
You are talking about two problems -
1. Send email from c#
2. Delete from table
For #1, refer here http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8
try
{
var fromAddress = new MailAddress("address#gmail.com", "Support");
var toAddress = new MailAddress(user.email, user.username);
const string subject = "Processing";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("username", "password")
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = dataToSend
})
{
message.IsBodyHtml = true;
smtp.Send(message);
}
}
catch (Exception)
{
Model.Message = "Exception Occured During Mail Sending";
}

How do I tell my application to send an email?

How would I tell my application to send an email to a subscriber? How would I send it every week starting from the time the appication is launched.
Use the classes in the System.Net.Mail namespace to send emails from your app. Specifically, the MailMessage class serves this purpose.
To send emails periodically, you can use a timer (use System.Timers.Timer, for example) or you could use the built-in Windows task scheduler, which has rich functionality and runs as a service, so that you don't need to keep an interactive session open on your machine. I can give you a more detailed answer if you provide more details about the type of app you're developing.
I use this method to use gmail, and it works well for me.
var fromAddress = new MailAddress("From");
var toAddress = new MailAddress("To");
string fromPassword = textBox4.Text;
const string subject = "Test";
const string body = "Test Finished";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
Attachment attachf = new Attachment("C:\\file.txt");
message.Attachments.Add(attachf);
smtp.Send(message);
}
}
I haven't created anything directly like this myself but I've seen solutions that use a Scheduled Task on the Server which are set to run on a certain date/time a small script that carries out what is required. This assumes you have your own server though...

Categories