Hi samsmuthu,
Refer below code. This code will try 5 time to send email if failed.
You can modify as per your need.
C#
protected void SendInquiryMail()
{
int tryCount = 5;
bool failed = false;
do
{
try
{
failed = false;
string MailIDs;
MailIDs = "xxx@xxxx.net" + "," + txtEmail.Text;
MailMessage mm = new MailMessage();
mm.From = new MailAddress("noreply@xxxx.net");
for (int i = 0; i < MailIDs.Split(',').Length; i++)
{
mm.To.Add(new MailAddress(MailIDs.Split(',')[i]));
}
mm.Subject = TxtPosition.Text + " - SATHUTA.NET Online Apply";
mm.Body = "Dear Applicant," + "<br /><br />" + "Your application has been submitted to HR department and they will contact you directly after shortlisted." + "<br /><br />" + "Thank you.";
if (FileUpload1.HasFile)
{
string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
mm.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "xxx.xxx.xxx.xxx";
// smtp.EnableSsl = True
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "noreply@xxxx.net";
NetworkCred.Password = "xxxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
}
catch (Exception ex)
{
failed = true;
tryCount = tryCount - 1;
}
finally
{
TxtMessage.Text = "";
LblFileName.Text = "";
LblMassage.Visible = true;
}
}
while (failed && tryCount > 0);
}
VB.Net
Protected Sub SendInquiryMail()
Dim tryCount As Integer = 5
Dim failed As Boolean = False
Do
Try
failed = False
Dim MailIDs As String
MailIDs = "xxx@xxxx.net" & "," & txtEmail.Text
Dim mm As New MailMessage()
mm.From = New MailAddress("noreply@xxxx.net")
For i As Integer = 0 To MailIDs.Split(","c).Length - 1
mm.To.Add(New MailAddress(MailIDs.Split(","c)(i)))
Next
mm.Subject = TxtPosition.Text & " - SATHUTA.NET Online Apply"
mm.Body = "Dear Applicant," & "<br /><br />" & "Your application has been submitted to HR department and they will contact you directly after shortlisted." & "<br /><br />" & "Thank you."
If FileUpload1.HasFile Then
Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
mm.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileName))
End If
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "xxx.xxx.xxx.xxx"
'smtp.EnableSsl = True
Dim NetworkCred As New System.Net.NetworkCredential()
NetworkCred.UserName = "noreply@xxxx.net"
NetworkCred.Password = "xxxxxxx"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 25
smtp.Send(mm)
Catch ex As Exception
failed = True
tryCount = tryCount - 1
Finally
TxtMessage.Text = ""
LblFileName.Text = ""
LblMassage.Visible = True
End Try
Loop While failed AndAlso tryCount > 0
End Sub