link email address and send email via outlook

前端 未结 1 1325
别那么骄傲
别那么骄傲 2021-01-29 02:48

I am working on a wpf app, and I have a Customer Information section where I can record my customer information. In this section, I use a textbox recording customer\'s email add

相关标签:
1条回答
  • 2021-01-29 03:49

    I use this method to send my e-mails... note that this is not specifically for Outlook... it will use whichever software is the default e-mail program that is set on the user's computer:

    public bool SendEmail(List<string> toAddresses, List<string> ccAddresses, string fromAddress, string emailSubject, string emailBody, bool isBodyHtml)
    {
        MailMessage email = new MailMessage();
        email.From = new MailAddress(fromAddress);
        foreach (string address in toAddresses) email.To.Add(new MailAddress(address));
        foreach (string address in ccAddresses) email.CC.Add(new MailAddress(address));
        email.BodyEncoding = Encoding.UTF8;
        email.IsBodyHtml = false;
        email.Subject = emailSubject;
        email.Body = emailBody;
        email.Priority = MailPriority.Low;
        SmtpClient smtpClient = new SmtpClient(Settings.Default.DefaultEmailServerPath);
        smtpClient.Credentials = new NetworkCredential(Settings.Default.EmailNetworkCredentialUserName, Settings.Default.EmailNetworkCredentialPassword, Settings.Default.EmailNetworkCredentialDomain);
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.Host = Settings.Default.DefaultEmailServerPath;
        smtpClient.UseDefaultCredentials = true;
        try { smtpClient.Send(email); }
        catch { return false; }
        return true;
    }
    

    Note that I have overloaded methods for this, so this one has all the options in it... you can freely remove several lines if you prefer. There is also a shortcut way of sending an e-mail:

    System.Diagnostics.Process.Start("mailto:youremail@yourcompany.com");
    

    Basically, I would add either a HyperLink control, or a Button that has a Command into your UI and then call this code from your handler. You can find out more about the HyperLink control from the Hyperlink class page at MSDN and there is a good example found in this post.

    UPDATE >>>

    You really should provide code examples... I have no idea how you have set up your TextBox, whether you are binding or not, the names of the parameters and so forth. As such, I can only make assumptions that you will have to relate to your own code.

    First, add a Hyperlink control in the same place as your TextBox:

    <TextBox Grid.Row="0" Grid.Column="1" Name="EmailTextBox" Text="{Binding Email}" 
        Visibility="{Binding IsValidEmail, Converter={StaticResource 
        InverseBoolToVisibilityConverter}}" />
    <TextBlock Grid.Row="0" Grid.Column="1">
        <Hyperlink RequestNavigate="Hyperlink_RequestNavigate">
            <TextBlock Text="{Binding Text, ElementName=EmailTextBox}" Visibility="{
        Binding IsValidEmail, Converter={StaticResource BoolToVisibilityConverter}}" />
        </Hyperlink>
    </TextBlock>
    

    You see the basic idea here is to have the two controls share one UI location and 'take turns' to be visible depending on the value of the TextBox. Therefore, you'll need to add a bool property (IsValidEmail in my example) that you set to true when the text value is a valid e-mail address. Then the BoolToVisibilityConverter will convert that true value to Visibility.Visible for the Hyperlink control and the InverseBoolToVisibilityConverter will convert that false value to Visibility.Collapsed or Visibility.Hidden for the Hyperlink control. I hope and trust that you can work the rest out yourself as my time is limited today.

    0 讨论(0)
提交回复
热议问题