Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
MailMessage message = new MailMessage();
MailAddress from = new MailAddress(s@test.com, "suresh");

SmtpClient client = new SmtpClient("Server Ip Address")
client.Credentials = new NetworkCredential("UN","Pwd");
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
client.Send(message);


When i sending mail, some of the client doesn't getting mail.
I am getting some Undeliverable Messages:

1.You do not have permission to send to this recipient. For assistance, contact your system administrator.
2.Could not deliver the message in the time limit specified. Please retry or contact your administrator.
3.The destination server for this recipient could not be found in Domain Name Service (DNS). Please verify the email address and retry. If that fails, contact your administrator.

Please send me the solution.

Thanks,

Suresh.
Posted
Updated 20-Dec-11 2:08am
v2

Hi,

Follow this process to sending mail..

http://forums.asp.net/t/971802.aspx[^]
 
Share this answer
 
Comments
Member 4391175 20-Dec-11 8:28am    
Hi Jitendra,
My code is working perfectly to send emails,..most of the members are getting mail but my problem is that..some of the members are not getting Email.. so then
I am getting some Undeliverable Messages:

1.You do not have permission to send to this recipient. For assistance, contact your system administrator.
2.Could not deliver the message in the time limit specified. Please retry or contact your administrator.
3.The destination server for this recipient could not be found in Domain Name Service (DNS). Please verify the email address and retry. If that fails, contact your administrator.

Please send me the solution.

Thanks,

Suresh.
Jitendra Parida - Jeetu 21-Dec-11 1:35am    
Hi,
When we are sending email continuously, some emails are spam on recipient email address. so that when you send mails give time gape to send on to another.
Hi,
I guess, the problem with the smtp settings.Go through following link.
click here[^]
 
Share this answer
 
That's a very basic code,
It should work if everythings is correct, but it seems that the problem may be in checking the destination email if it really exists.

I have a pretty good function for that which get rid of most of the common errors by checking with regular expressions and mxrecords:

C#
public static bool CheckEmail(String myContact,bool securesent)
        {   //regular expression pattern for valid email
            //addresses, allows for the following domains:
            //com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tvç
            string pattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
         @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
         @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
             
            Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
            bool valid = false;
            if (string.IsNullOrEmpty(myContact))
            {
                valid = false;
            }
            else
            {
                valid = check.IsMatch(myContact);
                if (!valid)
                {
//this one  is  a personal function to keep track of errors  you can ignore it
                    CreateLogFiles Log = new CreateLogFiles();
                    Log.ErrorLog("C:/", "FORMAT Error : NAME=(" + name + ") \t ToAddress=(" + toMail + ")", "FORMATErrors.txt");
                }
                if (securesent)
                {
                    try
                    {
                        string[] host = (myContact.Split('@'));
                        string hostname = host[1];
                        string[] MX = DnsMx.GetMXRecords(hostname);
                        IPHostEntry IPhst = Dns.GetHostEntry(MX[0]);

                        IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
                        Socket s = new Socket(endPt.AddressFamily,
                                SocketType.Stream, ProtocolType.Tcp);
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
            }
            return valid;
        }



After checking the email it's pretty much what you did, but you can still improve by sending it async. so that you can get error info from exchange server in case you are using one.


C#
MailMessage mail = new MailMessage();                        
                        MailAddress toMA = new MailAddress("email@domain.com","NAME");
                        MailAddress FromMA;
                        FromMA = new MailAddress("email@domain.com","NAME");
                        mail.Headers.Add("Return-Path", "email@domain.com");
                        mail.ReplyTo = FromMA;
                        client.Credentials = new System.Net.NetworkCredential("USERLOGIN", "USERPWD");
                        mail.To.Add(toMA);                        
                        mail.From = FromMA;
                        //mail.Sender = FromMA;
                        mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                        mail.SubjectEncoding = Encoding.UTF8;                     
                        
                        mail.Subject = subject; //string variable
                        mail.BodyEncoding = Encoding.UTF8;
                        mail.Body = body; //string variable
                        mail.IsBodyHtml = true;
                        mail.Priority = MailPriority.High;//optional

                        
                        //  SEND MAIL
                        if (securesent)
                            {
                                client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);//do something after sending (log's)
                                client.SendAsync(mail, mail.To[0].ToString());
                            }
                            else
                            {
                                client.Send(mail);//normal sending                               
                            }                            
                        }                        
                    }
                    catch (Exception ex)
                    {                                     
                        return ex.Message;
                    }
 
Share this answer
 
Comments
Member 4391175 21-Dec-11 9:05am    
Thanks for the reply,
i will apply this logic, and lets see what happens.

Thanks,
Suresh
Member 4391175 21-Dec-11 9:48am    
Hi ,
I am getting error :Doesn't contain defenation DnsMx.GetMXRecords
string[] MX = DnsMx.GetMXRecords(hostname);
There is no method regarding Dns.GetMXRecords(hostname) or
DnsMx.GetMXRecords(hostname);

Thanks,
Suresh
Mendor81 22-Dec-11 6:40am    
My fault, that is a user created class

here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;

public class DnsMx
{
public DnsMx()
{
}
[DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

public static string[] GetMXRecords(string domain)
{

IntPtr ptr1 = IntPtr.Zero;
IntPtr ptr2 = IntPtr.Zero;
MXRecord recMx;
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
ArrayList list1 = new ArrayList();
try
{

int num1 = DnsMx.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
if (num1 != 0)
{
if (num1 == 9003)
{
list1.Add("Entrada DNS no Existe");
}
else
{
throw new Win32Exception(num1);
}
}
for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
{
recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
if (recMx.wType == 15)
{
string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange);
list1.Add(text1);
}
}
}
finally
{
DnsMx.DnsRecordListFree(ptr1, 0);
}
return (string[])list1.ToArray(typeof(string));
}

private enum QueryOptions
{
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
DNS_QUERY_BYPASS_CACHE = 8,
DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
DNS_QUERY_NO_HOSTS_FILE = 0x40,
DNS_QUERY_NO_LOCAL_NAME = 0x20,
DNS_QUERY_NO_NETBT = 0x80,
DNS_QUERY_NO_RECURSION = 4,
DNS_QUERY_NO_WIRE_QUERY = 0x10,
DNS_QUERY_RESERVED = -16777216,
DNS_QUERY_RETURN_MESSAGE = 0x200,
DNS_QUERY_STANDARD = 0,
DNS_QUERY_TREAT_AS_FQDN = 0x1000,
DNS_QUERY_USE_TCP_ONLY = 2,
DNS_QUERY_WIRE_ONLY = 0x100
}

private enum QueryTypes
{
DNS_TYPE_MX = 15
}

[StructLayout(LayoutKind.Sequential)]
private struct MXRecord
{
public IntPtr pNext;
public string pName;
public short wType;
public short wDataLength;
public int flags;
public int dwTtl;
public int dwReserved;
public IntPtr pNameExchange;
public short wPreference;
public short Pad;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900