Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:48:26+00:00 2026-06-14T16:48:26+00:00

I was trying to create a function that emails to target address when used.

  • 0

I was trying to create a function that emails to target address when used.

my email model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Security;

namespace myspecial.net.Models
{
    public class EmailModel
    {
        // Sender email and recipient email
        public MailAddress Sender { get; set; }
        public IEnumerable<MailAddress> Recipient { get; set; }
        // Subject and message
        public String Subject { get; set; }
        public String Body { get; set; }
        // Attachment Files
        public String AttachmentPath { get; set; }
        // Login Information
        public String UserName { get; set; }
        public String Password { get; set; }
        // Simple Mail Transfer Protocol (SMTP) provider and port
        public String SmtpServer { get; set; }
        public Int32 SMTPport { get; set; }
        // Post Office Protocol (POP) version 3 provider and port
        public String POP3Server { get; set; }
        public Int32 POP3port { get; set; }
        // Secure Sockets Layer
        public Boolean SSL { get; set; }
    }
}

my controller:

/// <summary>
/// E-Mail's to the address. Before using this function fill the email model.
/// </summary>
public void Email(EmailModel mailer)
{
    MailMessage mail = new MailMessage();
    // Standart required mail information
    mail.From = mailer.Sender;
    mail.Sender = mailer.Sender;
    // Recipients
    foreach (MailAddress rcpnt in mailer.Recipient.ToList())
    { mail.To.Add(rcpnt); }
    // Subject
    mail.Subject = mailer.Subject;
    // Body
    mail.Body = mailer.Body;
    // Optional Attachment
    if (mailer.AttachmentPath != null || mailer.AttachmentPath.Trim() != "")
    {
        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment(mailer.AttachmentPath.Trim());
        mail.Attachments.Add(attachment);
    }
    // Important (Simple Mail Transfer Protocol)
    SmtpClient SmtpServer = new SmtpClient(mailer.SmtpServer,mailer.SMTPport);
    SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
    SmtpServer.Credentials = new System.Net.NetworkCredential(mailer.UserName, mailer.Password, mailer.SmtpServer);
    SmtpServer.Port = mailer.SMTPport;
    SmtpServer.EnableSsl = mailer.SSL;
    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
    // Send Mail
    SmtpServer.Send(mail);
}

how i use it :

        // Send as mail
        EmailModel mail = new EmailModel();
        // Mail addresses to send
        MailAddress[] addressler = new MailAddress[] { new MailAddress("berkeryuceer@yahoo.com") };
        // Options here
        mail.SMTPport = 465; // Port // 465 // 26 // 25 // 366 // 587
        mail.SmtpServer = "smtp.gmail.com"; // Server
        mail.UserName = "MyGmailAddress@gmail.com"; // UserName
        mail.Password = "**********"; // Password 
        mail.SSL = true; // Secure Sockets Layer
        // Mail
        mail.Sender = new MailAddress("MyGmailAddress@gmail.com"); // Sender (From)
        mail.Recipient = addressler; // Recipient (To)
        mail.Subject = "Some subject here..";
        mail.Body = "Some blabla bla blablabla blabla here...";
        // Optional Attachment // Attach excel file. 
        mail.AttachmentPath = ExportToExcelForMail(id).ToString(); 
        Email(mail);

In my web project create a report as excel file and wanna send it to my self attached to an email. I’m not sure what’s wrong or what i am missing here.. Everything seems valid to me but still i get this error :

MY ERROR IMAGE

Stack Trace:

System.Net.Mail.SmtpFailedRecipientException was unhandled by user code
  Message=Posta kutusu kullanılamıyor. Sunucu yanıtı şöyleydi: 5.7.1 <MyGmailAddress@gmail.com> Access to <berkeryuceer@yahoo.com> not allowed
  Source=System
  FailedRecipient=<berkeryuceer@yahoo.com>
  StackTrace:
       konum: System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
       konum: System.Net.Mail.SmtpClient.Send(MailMessage message)
       konum: blabla.net.Controllers.OtelFormsController.Email(EmailModel mailer) D:\bla\bla\blabla.net\blabla.net\Controllers\OtelFormsController.cs içinde: satır 753
       konum: blabla.net.Controllers.OtelFormsController.Close(Int32 id) D:\bla\bla\blabla.net\blabla.net\Controllers\OtelFormsController.cs içinde: satır 691
       konum: lambda_method(Closure , ControllerBase , Object[] )
       konum: System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       konum: System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       konum: System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       konum: System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       konum: System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: 

so what am i missing here? whats wrong? how can i solve this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:48:27+00:00Added an answer on June 14, 2026 at 4:48 pm

    You can use following code:

        /// <summary>
        /// Sends mail with authentification
        /// </summary>
        /// <param name="recipients"></param>
        /// <param name="from"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isHTMLbody"></param>
        /// <param name="SMTPhost"></param>
        /// <param name="priority"></param>
        /// <param name="credentials"></param>
        /// <param name="port"></param>
        /// <param name="enableSsl"></param>
        static void SendMailWithAuthentication(List<string> recipients, string from, string subject, string body, bool isHTMLbody,
            string SMTPhost, System.Net.Mail.MailPriority priority, System.Net.NetworkCredential credentials, int? port, bool enableSsl)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            foreach (string recipient in recipients)
            {
                message.To.Add(recipient);
            }
            message.SubjectEncoding = Encoding.UTF8;
            message.BodyEncoding = Encoding.UTF8;
            message.Subject = subject;
            message.From = new System.Net.Mail.MailAddress(from);
            message.IsBodyHtml = isHTMLbody;
            message.Body = body;
            message.Priority = priority;
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Credentials = credentials;
            client.Host = SMTPhost;
            if (port != null)
            {
                client.Port = (int)port;
            }
            client.EnableSsl = enableSsl;
            client.Send(message);
            message.Dispose();
        }
    

    And this is how you should use it for gmail:

            string subject = "my subject";
            string body = "my html body";
            int port = 587;
            string SMTPhost = "smtp.gmail.com";
            string sender = "my@email.com";
            System.Net.Mail.MailPriority mPriority = System.Net.Mail.MailPriority.Low;        
            List<string> recipients = new List<string>(){"some@reciever.com"};
            bool enableSSL = true;
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("UserName","Password");
            SendMailWithAuthentication(recipients, sender, subject, body, true,
                SMTPhost,
                mPriority, credentials,
                enableSSL);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m using VBA in Outlook 2010 and I’m trying to create a function that
I'm trying to create a function that would append two vectors together, but I
I am trying to create a function that updates another value, and initially (after
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create a function that will block access to some of
I'm trying to create a function that would dynamically allocate an array, sets the
I am trying to create a function that given a divid, and a list
I'm trying to create a function that will simply allow me to pass an
I am trying to create a function that sends 100 call per second, and
I’m trying to learn Haskell and I was trying to create a function that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.