I'm trying to add email capability to my IOS project made with Xamarin Forms. I'm using DependencyService to implement the email function and my IOS code is as follows:
public void SendEmail(string[] recipients, string subject, string body, string filePath, string fileName)
{
MFMailComposeViewController mailController;
if (MFMailComposeViewController.CanSendMail)
{
mailController = new MFMailComposeViewController();
mailController.SetToRecipients(recipients);
mailController.SetSubject(subject);
mailController.SetMessageBody(body, false);
mailController.AddAttachmentData(NSData.FromFile(filePath), "application/pdf", fileName);
mailController.Finished += (object s, MFComposeResultEventArgs args) =>
{
Console.WriteLine(args.Result.ToString());
args.Controller.DismissViewController(true, null);
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mailController, true, null);
}
}
but when I test the implementation, nothing happens no errors or anything. I can continue to use the app as normal after the failed attempt to email. I understand this question has been asked here and here but I have already enabled an email account on my testing iPad's default mail app and have removed all instances of DisplayActionSheet in my PCL implementation.
Related
I'd like to send a slash command to a channel of my choice.
I'm using the Newtonsoft Json.NET serializer from NuGet. Currently, I have the following code:
string messageToSend = #"/Kyber test task here";
string channelToSendTo = "#general";
var urlWithAccessToken
= "https://hooks.slack.com/services/TOKEN/SPECIFIC/STUFF";
var client = new SlackClient(urlWithAccessToken);
client.PostMessage(username: "MyBot",
text: messageToSend,
channel: channelToSendTo);
And the SlackClient.cs
public SlackClient(string urlWithAccessToken)
{
_uri = new Uri(urlWithAccessToken);
}
public void PostMessage(string text, string username = null, string channel = null)
{
Payload payload = new Payload()
{
Channel = channel,
Username = username,
Text = text
};
PostMessage(payload);
}
public class Payload
{
[JsonProperty("channel")]
public string Channel { get; set; }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}
It currently just says "/kyber test task here" instead of calling the /kyber slash command.
I've seen the undocumented chat.command command, but it doesn't appear to work with the Slack API now. I was hoping there was another way to do it without using the Legacy App functionality, since it surprises me that I was unable to find an example newer than about 3-4 years ago that didn't use it.
I guess this is the answer, but I asked Slack support about it. They replied:
It sounds like you might be asking about allowing an app to send a message containing the slash command, in order to trigger the slash command.
If so, I'm afraid that's not possible. Instead, since all slash commands essentially trigger a specific API endpoint, you'll need to contact the developers of that slash command to see if they make that endpoint publicly accessible so that you can create your own app mechanism to try and trigger commands to those endpoints.
I'm sorry that I don't have better news for you on this. I hope that info might help you to move forward though. Thanks for checking in on this.
So I think the answer is that the App I'm using doesn't support that functionality, and Slack doesn't/won't either. Thanks anyways for the help.
This question already has answers here:
send email in c#
(7 answers)
Closed 3 years ago.
I want to get help with how to send the console output via email on a click button function in my program.
The variable textBox2.Text contains the text that is being printed out to the console and I want this text to be send automatically on the (button1_Click_1) function.
I have found some solutions on somewhat similar questions but none of them seem to work, I hope I can find a solution here.
My code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
Console.WriteLine(textBox2);
}
private void button1_Click_1(object sender, EventArgs e)
{
//Sending email function with the console output that is being printed from (textBox2.Text) should be here.
Taskbar.Show();
System.Windows.Forms.Application.Exit();
}
Microsoft recommends that MailKit and MimeKit libraries be used for sending emails from C# applications.
Here is the working code snippet in C# for sending an email:
// File name: SendMail.cs
using System;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;
namespace SendMail {
class Program
{
public static void Main (string[] args) {
using (var client = new SmtpClient ()) {
// Connect to the email service (Accept ssl certificate)
client.ServerCertificateValidationCallback = (s,c,h,e) => true;
client.Connect ("smtp.friends.com", 587, false);
// Optional step: Send user id, password, if the server requires authentication
client.Authenticate ("emailID", "emailPassword");
// Construct the email message
var message = new MimeMessage();
message.From.Add (new MailboxAddress ("Sender's Name", "sender-email#example.com"));
message.To.Add (new MailboxAddress ("Receiver's Name", "receiver-email#example.com"));
message.Subject = "Automatic email from the application";
message.Body = new TextPart ("plain") { Text = #"Hello Customer, Happy new year!"};
// Send the email
client.Send (message);
// Close the connection with the email server
client.Disconnect (true);
}
}
}
}
More information:
https://github.com/jstedfast/MailKit
So I have this button in my Portable Class Library that should directly attach an xml file I've created into a mail and send it using the Messaging PlugIn. The problem is that .WithAttachment() is not supported in PCL so I wanted to ask if I can get around this using DependencyService and if so, how?
Can I just return .WithAttachment() from the UWP class (as UWP is my target platform)? Wouldn't there be a conflict because I've read that the overload of .WithAttachment() in UWP is .WithAttachment(IStorageFile file).
private void Senden_Clicked(object sender, EventArgs e)
{
var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (emailMessenger.CanSendEmail)
{
var email = new EmailMessageBuilder()
.To("my.address#gmail.com")
.Subject("Test")
.Body("Hello there!")
//.WithAttachment(String FilePath, string ContentType) overload showing in PCL
//.WithAttachment(IStorageFile file) overload for the UWP according to the documentation
.Build();
emailMessenger.SendEmail(email);
}
}
EDIT:
I've been able to modify Nick Zhou's answer a little bit to be able to send an email with attachment via button-click. I just changed this peace of code:
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add("*");
to this:
StorageFolder sf = ApplicationData.Current.LocalFolder;
var file = await sf.GetFileAsync("daten.xml");
Of course you then need to create the file inside the app's local folder instead of the documents library.
The problem is that .WithAttachment() is not supported in PCL so I wanted to ask if I can get around this using DependencyService and if so, how?
Of course you can use DependencyService to achieve sending email with attactment. But you could create two interface like code behind.
SendEmail Interface
public interface IMessageEmail
{
void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null);
}
IMessageEmail implementation in UWP project.
public void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null)
{
var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (emailMessenger.CanSendEmail)
{
if (attactment != null)
{
var email = new EmailMessageBuilder()
.To(address)
.Subject(subject)
.Body(body)
.WithAttachment(attactment)
.Build();
emailMessenger.SendEmail(email);
}
else
{
var email = new EmailMessageBuilder()
.To(address)
.Subject(subject)
.Body(body)
.Build();
emailMessenger.SendEmail(email);
}
}
}
As you can see the .WithAttachment(attactment) parameter is IStorageFile. So you need pass a file to the method. Hence you could create another DependencyService.
IFilePicker Interface
public interface IFilePicker
{
Task<StorageFile> getFileAsync();
}
IMessageEmail implementation in UWP project.
public async Task<StorageFile> getFileAsync()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
};
picker.FileTypeFilter.Add("*");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
return file;
}
return null;
}
You can try the project I have upload to github.
I'm using Quickblox C# SDK. I want to send message to a specific dialog. It's not well documented in Xamarin specific documentation. I decided to visit REST API documentation. As I could learn from there
By using Chat 2.0, you are not automatically storing your messages. Also a dialog entity won't be created/updated without saving a message to history.
I can infer if I set save_to_history to 1, chat dialog will be automatically created and message will be stored in the backend. However I couldn't figure out how I should specify that in C# SDK, cause extraParam in this method signature
public void SendMessage(int userId, string body, string extraParams, string dialogId, string subject = null, Quickblox.Sdk.Modules.ChatXmppModule.Models.MessageType messageType = Quickblox.Sdk.Modules.ChatXmppModule.Models.MessageType.Chat)
is just a string. I've dug into disassembled code and after some investigation understood that internally this parameter is used as XML so I tried these two options
var extraParams = "<extraParams> " +
"<save_to_history>1</save_to_history> " +
"</extraParams>";
And Also
var extraParams = "<save_to_history>1</save_to_history> ";
But none of these worked.
Anybody has idea how I should specify the extraParam?
Regards
The issue was simply that I forgot to call connect before I was sending a message.
Here is the method to send a message
public async Task SendMessageAsync(IUser sender, IChatMessage message, string channelID, CancellationToken token)
{
await loginIfRequired(sender, token);
var jsonMessage = JsonConvert.SerializeObject(message);
var recipientID = await getQuickbloxUserId(message.RecipientID, token);
var extraParams = "<extraParams> " +
"<save_to_history>1</save_to_history> " +
"</extraParams>";
_quickblox.ChatXmppClient.SendMessage(recipientID, jsonMessage, extraParams, channelID);
}
Inside loginIfRequired I call
_quickblox.ChatXmppClient.Connect(_currentUserID.Value, password);
And everything worked fine and the dialog was created.
Hope this will help someone.
I want to send SMS programatically for that i am using below code but seems that it dontwork in 8.1
SmsComposeTask SMSCompose = new SmsComposeTask();
SMSCompose.To = "<Number to which the SMS needs to be sent";
SMSCompose.Body = "Message that needs to be sent";
SMSCompose.Show();
Is there any othey way to achive it?
public async static Task SendMessage (string phoneNumber)
{
ChatMessage msg = new ChatMessage();
msg.MessageKind = Windows.ApplicationModel.Chat.ChatMessageKind.Standard;
msg.Body = "...";
msg.Recipients.Add(phoneNumber);
ChatMessageStore cms = await ChatMessageManager.RequestStoreAsync();
cms.SendMessageAsync(msg);
}
This should send the message on Windows Phone 8.1. You may want to check if cms is not null, just in case...
You should do some searching before make a new thread. Your question is already a part in this thread. The universal app is also called windows runtime, so same solution in that threat if you want to send message or event call or send email.
You can use this for send message:
Windows.ApplicationModel.Chat.ChatMessage msg = new Windows.ApplicationModel.Chat.ChatMessage();
msg.Body = "This is body of demo message.";
msg.Recipients.Add("10086");
msg.Recipients.Add("10010");
await Windows.ApplicationModel.Chat.ChatMessageManager.ShowComposeSmsMessageAsync(msg);
Enjoy coding!
P/s: update from #Sinaesthetic , this just compose the message, it'll not send it, all user has to do is to hit the send button :)